In scriptrunner cloud postunction, how to access subtask custom field values

Mordechai Cikk February 20, 2017

Hi Guys.

From a task postfunction, I need to access custom fields in the subtasks.

I am using scriptrunner cloud postfunction scripting option.

This is the code that loops through the subtasks and attempts (unsuccessfully) to catch the values of 3 custom fields:

// loop through subtasks and accumulate table information
subtasks.each { subtask ->
logger.info(subtask.toString())

resources = issue.fields[resourcesCFId] as String //Total hours of HTS equipment used (numeric)
charged = issue.fields[chargedCFId] //Is Charged (checklist)
order = issue.fields[orderCFId] //Order Number (text)
logger.info("resources1: ${resources} ")
logger.info("charged: ${charged} ")
logger.info("order: ${order} ")

temp = ' \n ' + '|' + 'xxx' + '|' + resources + '|' + charged + '|' + order + '|';

logger.info("temp: ", temp.toString())

output = output + temp
logger.info("output: ", output.toString())
}

 

From the results I see that I am looping through the subtasks but I seem to be catching the values from the parent and not the subtasks.

When I try "subtask.fields", it is not accepted.

image2017-2-21 12:30:42.png

 

I tried rest api but there was not successful getting to the subtasks.

I would truly appreciate some help here with working syntax.

 

Thanks!

 

2 answers

5 votes
Jon Bevan [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.
February 21, 2017

Hi Mordechai,

The following code should work:

def subtasks = issue.fields.subtasks
def output = ''
 
// loop through subtasks and accumulate table information
subtasks.each { subtask ->
    // The subtasks provided in a post function don't have all of the fields, we can retrieve the full subtask like this
    def subtaskWithFields = get(subtask.self).asObject(Map).body
    logger.info(subtaskWithFields.toString())
    def resources = subtaskWithFields.fields[resourcesCFId] as String //Total hours of HTS equipment used (numeric)
    def charged = subtaskWithFields.fields[chargedCFId] //Is Charged (checklist)
    def order = subtaskWithFields.fields[orderCFId] //Order Number (text)
    logger.info("resources1: ${resources} ")
    logger.info("charged: ${charged} ")
    logger.info("order: ${order} ")

    def temp = ' \n ' + '|' + 'xxx' + '|' + resources + '|' + charged + '|' + order + '|';
    logger.info("temp: ", temp.toString())

    output = output + temp
    logger.info("output: ", output.toString())
}
Mordechai Cikk February 21, 2017

Thanks Jon for coming back on this.

Adding the "def subtaskWithFields = get(subtask.self).asObject(Map).body" line doesn't make it past the error checker.

image2017-2-21 16:4:9.png

I am copying the full code (without the subtaskWithFields line) below in case something in the code is causing problems. 

If I may add a questions (to use after the subtask problem is resolved).

To verify that the parent field values were in fact being being picked up for each subtask I gave those fields values in the Parent.

The resurces and order fields displayed the correct parent value.

The charged field which is a checklist did not display well. How can I correctly capture the checklist value?

 

Thanks for your help.

 

The full code:

// render subtask data in table format.
logger.info('Starting post function')
def resourcesCFId = 'customfield_14602'
def chargedCFId = 'customfield_15200'
def orderCFId = 'customfield_15201'
String resources
String charged
String order
def outputCfId = 'customfield_15700'
def output = '||Subtask||Resource hrs||Charged||Order #||'
// def temp
def subtasks = issue.fields.subtasks // note that you had sub-tasks
// loop through subtasks and accumulate table information
subtasks.each { subtask ->
logger.info(subtask.toString())
resources = issue.fields[resourcesCFId] as String //Total hours of HTS equipment used (numeric)
charged = issue.fields[chargedCFId] //Is Charged (checklist)
order = issue.fields[orderCFId] //Order Number (text)
logger.info("resources1: ${resources} ")
logger.info("charged: ${charged} ")
logger.info("order: ${order} ")

//resources = '12345' //for testing
logger.info("resources2: ", resources.toString() )
// temp = ' \n ' + '|' + resources + '|'
def temp = ' \n ' + '|' + 'xxx' + '|' + resources + '|' + charged + '|' + order + '|';
logger.info("temp: ", temp.toString())

output = output + temp
logger.info("output: ", output.toString())

}
if (output == (issue.fields[outputCfId] as String )) { 
logger.info("already been updated")
return
}
issue.fields[outputCfId] = output
put("/rest/api/2/issue/${issue.key}") 
// .queryString("overrideScreenSecurity", Boolean.TRUE) 
.header("Content-Type", "application/json")
.body([
fields:[
(outputCfId): output
]
])
.asString()
Jon Bevan [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.
February 21, 2017

Hi Mordechai, there are some known problems with the error checker that we're actively working to resolve. It sometimes complains about code which we know is fine.

The biggest problem I can see in your full code is that you never seem to read anything from the subtask issues - is this your intention?

My suggested code above "fixes" that...

Mordechai Cikk February 21, 2017

Yes. Absolutely does. I appreciate all the help.

All subtask value are displaying correcty in the table except for the charged field value which is a checklist item and I am not accessing it correctly. Only the default value is displaying even though the issues have other values.

This is the output table on the issue screen.

image2017-2-22 10:58:22.png

Can you please show or point me to how to correctly extract the checkbox vaue?

below is the code I used so far.

def subtaskID = subtaskWithFields.key
  		resources = subtaskWithFields.fields[resourcesCFId]
  		charged = subtaskWithFields.fields[chargedCFId].value //checkbox
  		order = subtaskWithFields.fields[orderCFId]


Many thanks

 

Mordechai Cikk February 21, 2017

Hi Jon,

All works.

Just writing my problem above led me to the solution based on your previous assistance.

All subtask data displayed correctly.

Thanks for you patience and help!

Mordechai

Jon Bevan [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.
February 22, 2017

No problem, glad you got it sorted!

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2017

In your code in order to get the sub-tasks of an issue you should try

issue.getSubTaskObjects(), it returns a collection of "Issue" and it should work.

I think you are trying issue.getSubTasks () but this returns List<GenericValue> and is deprecated. and this also requires the subtask objects to be cast to Issue type in order to use subtask.field thus it isn't working.

 

Jon Bevan [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.
February 21, 2017

The OP is using JIRA Cloud, not JIRA Server, so the Java APIs are not available.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 21, 2017

Oh yes, Indeed, I missed the "jira-cloud" tag

Suggest an answer

Log in or Sign up to answer