Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Getting value of a customfield when a workflow is completed

Jamshaid
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2023

I have a workflow designed, and when it is clicked on the issue screen, it pops this window.

asdf.png

 

I want to get the field's value highlighted as 1  when the button highlighted as 2 is pressed. But I get the value of the state the screen was shown. Is there any way I could get the value after making changes to the field 1 values? 
I am unable to find anything regarding this.

 

Here is the script I am using in script runner:

def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl")
log.setLevel(Level.DEBUG)

def issueManager = ComponentAccessor.issueManager
//def issue_key="TM-636" //for testing purpose only
//def issue_obj = issueManager.getIssueObject(issue_key) // for testing purpose only
def issue_key=issue
def issue_obj = issueManager.getIssueObject(issue.toString())
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_10501") //Custom Field Value - object TM_confirm_21,
def owner=issue_obj.getReporter();
ownerName=owner.getName();


def json = issue_obj.getCustomFieldValue(cField)
log.info(json)
ArrayList<String> questionList = new ArrayList();
ArrayList<String> answerList = new ArrayList();
String[] arr = json.split("fields\":");
System.out.println(arr.length);
for (String string : arr) {
String[] a = string.split("value\":");
for (String string1 : a) {
if (string1.startsWith("\"")) {
String content = string1.substring(1, string1.indexOf("\"}"));
if (content.equals("0396bc37-942d-4e7e-bd53-e77d8ae1d5c9")) {//yes id
answerList.add("Yes");//yes
} else if (content.equals("b0ff8ab7-7038-496c-bea7-05a46252847f")) {//no id
//no
answerList.add("No");
} else {
questionList.add(content);
}

}
}
}
HashMap<String, String> finalMap = new HashMap();
for (int i = 0; i < answerList.size(); i++) {
if (!answerList.get(i).equals("Yes")) {
finalMap.put(questionList.get(i), answerList.get(i));
}
}
for (Map.Entry<String, String> entry : finalMap.entrySet()) {
Object key = entry.getKey();
Object val = entry.getValue();
log.info("asad "+key.replace("\\n","\\\\n"))
log.info(val)
}

def authString = "username:password".getBytes().encodeBase64().toString()
int counter=0;

for (Map.Entry<String, String> entry : finalMap.entrySet()) {
counter+=1;
log.info("Counter "+counter)

String question="Security weakness identified in Threat Modeling. You can use the technical details below to address the weakness in technical design.\\n"
question += entry.getKey();
Object val = entry.getValue();
//question=question.replace("\\n", "\\r\\n");// new line character to encode for json
log.info("question: "+question)
def createSubTaskBody="{\"fields\":{\"project\":{\"key\": \"TM\"},\"parent\":{\"key\": \""+issue_key+"\"},\"summary\": \""+issue_key+"- Security Task ["+counter+"]\",\"description\": \""+question+"\",\"issuetype\":{\"id\": \"10003\"},\"assignee\":{\"name\":\""+ownerName+"\"}}}"
log.info(createSubTaskBody)
def connection1 = new URL("http://example.com/rest/api/2/issue/").openConnection() as HttpURLConnection
connection1.setRequestMethod( "POST" )
connection1.setRequestProperty( "Authorization", "Basic ${authString}" )
connection1.doOutput = true
connection1.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection1.getOutputStream().write(createSubTaskBody.getBytes("UTF-8"))
connection1.connect()
def postRC2 = connection1.getResponseCode();
if(postRC2==201)log.info("created successfully ") else log.info("Error creating sub-task "+postRC2)
//log.info(issue_key)
}
What this code does is explained here:
When I click the Complete Threat Modelling button mentioned as 1, it fetches the current value of Field mentioned as 2.
asdf.png

It then opens the following screen where I can change the answers to the question:

 

asdf.png

When I change the values mentioned in and click the button mentioned in 3, it takes the values of the field marked as 1. It should have taken the updated values rather than taking values in the previous screen. 

I am unable to get why the scripts behave this way.

Thanks

1 answer

1 vote
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2023

What do you mean by "get" the value?  And what are you going to do with it?

Jamshaid
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2023

Get value means that Field 1 have some values. This is a customfield that is a bundled field. A user can change its values. 

When I press the button on step 2, I want to get the updated answers of the question but what I get in the script are the old answers of the question. 

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2023

I am well aware of how fields work.

What I was asking is about what you mean by "get".  What are you getting and why?  Where is it going?

Jamshaid
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2023

@Nic Brough -Adaptavist-

II'm getting fetching all the answers of field 1 after the window is populated and user makes changes or not. 

I then need to pass it to a Script runner script to create subtasks based on some conditions. 

Currently, when the script runner receives the answers, these are from the initial state of field 1 and does mot include the changes a user makes. 

I hope that I'm able to clear my point. 

Thanks 

Jamshaid
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2023

@Nic Brough -Adaptavist-  I have updated my question adding more details. Please have a look at it.

Thanks

Jamshaid
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2023

I have found a workaround for this by adding a new state in workflow and it now works fine.

Thanks

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events