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

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

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())
}

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()

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...

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

 

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

No problem, glad you got it sorted!

0 vote
Tarun Sapra Community Champion Feb 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.

 

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

Tarun Sapra Community Champion Feb 21, 2017

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published 4 hours ago in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

69 views 4 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you