Get cf values from subtasks, add, post to parent

Illiterati December 4, 2017

I'm trying to use a Script Listener (Issue Updated Event) to get the values of customfield "Grade" for all the subtasks of that issue's parent, then post the sum of those values to the customfield "GPA" in the parent issue. (This is just 1 step in the process of actually calculating GPA.. I'm just stuck here and want to make sure I understand the concepts:) ) . Here's the code I have currently, based off the very sparse Scriptrunner for JIRA Cloud documentation:


//only process when issue is a subtasks
if (!issue.fields.issuetype.subtask) {
return
}


// Retrieve all the subtasks of this issue's parent
def parentKey = issue.fields.parent.key
def allSubtasks = get("/rest/api/2/search")
.queryString("jql", "parent=${parentKey}")
.queryString("fields", "customfield_10061")
.asObject(Map)
.body
.issues as List<Map>
logger.info("Total subtasks for ${parentKey}: ${allSubtasks.size()}")

// Sum the grades
def grade = 'customfield_10061' as Integer
def grades = allSubtasks.collect { Map subtask ->
subtask.fields.grade ?: 0
}.sum()
logger.info("Summed grades: ${grades}")

// Get the field ids
def fields = get('/rest/api/2/field')
.asObject(List)
.body as List<Map>

def GPA = fields.find { it.name == "GPA" }.id
logger.info("Custom field ID to update: ${GPA}")
 
// Now update the parent issue
def result = put("/rest/api/2/issue/${parentKey}")
.header('Content-Type', 'application/json')
.body([
fields: [
(GPA): grades
]
])
.asString()
 
// check that updating the parent issue worked
assert result.status >= 200 && result.status < 300


And here's the error I'm getting when I run the issue updated event on a relevant subtask. What am I missing??


2017-12-05 04:03:54.647 INFO - Serializing object into 'interface java.util.Map'

2017-12-05 04:03:55.345 INFO - GET /rest/api/2/search asObject Request Duration: 4022ms

2017-12-05 04:03:55.367 INFO - Total subtasks for EABST-11: 4

2017-12-05 04:03:56.044 ERROR - For input string: "customfield_10061" on line 17

2017-12-05 04:03:56.086 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null

 

 

 

2 answers

1 accepted

2 votes
Answer accepted
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.
December 7, 2017

Hi Illiterati,

I've modified your code a bit so that accessing the custom field value should be more successful:

//only process when issue is a subtasks
if (!issue.fields.issuetype.subtask) {
return
}

def grade = 'customfield_10061'

// Retrieve all the subtasks of this issue's parent
def parentKey = issue.fields.parent.key
def allSubtasks = get("/rest/api/2/search")
.queryString("jql", "parent=${parentKey}")
.queryString("fields", grade)
.asObject(Map)
.body
.issues as List<Map>
logger.info("Total subtasks for ${parentKey}: ${allSubtasks.size()}")

// Sum the grades

def grades = allSubtasks.collect { Map subtask ->
subtask.fields[grade] ?: 0
}.sum()
logger.info("Summed grades: ${grades}")

// Get the field ids
def fields = get('/rest/api/2/field')
.asObject(List)
.body as List<Map>

def GPA = fields.find { it.name == "GPA" }.id
logger.info("Custom field ID to update: ${GPA}")
 
// Now update the parent issue
def result = put("/rest/api/2/issue/${parentKey}")
.header('Content-Type', 'application/json')
.body([
fields: [
(GPA): grades
]
])
.asString()
 
// check that updating the parent issue worked
assert result.status >= 200 && result.status < 300

Let me know how you get on with that.

Thanks,
Jon

Illiterati December 7, 2017

Hi Jon,

Thanks works great! Thanks for your assistance!

Illiterati

0 votes
Daniel Yelamos [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.
December 5, 2017

Illiterati

This is a feature that will be released as a canned Scripted field, and it is currently under development by me!

You can follow it's progress if you watch this ticket.

I can't tell you when it will be released exactly, but maybe it is worth waiting for it to be released than to actually debug.

If you still want to do this, I can tell you how to do it, but maybe you'd like it better as a feature that is actually supported by us, unlike custom coding.

Cheers!

Dyelamos

Illiterati December 5, 2017

I'm unable to access that ticket. While I will definitely use the canned scripted field when it is available, I also would love to understand how to custom script this!

Daniel Yelamos [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.
December 6, 2017

Oh Illiterati. I'm so very sorry but I misread the title of your question. Your question involves JIRA cloud and I only work with JIRA server. I could still find out how to do this for you, although i don't think this is a feature in development for cloud. Give me a few days and I'll chase down the right person to help you.

Apologies

Dyelamos

Illiterati December 6, 2017

I see. No worries! That's my problem as well - I can accomplish this same result just fine on JIRA server. The rest calls and syntax for Scriptrunner for Cloud, however, is a different story. Trying to determine the most resource-efficient way to do this for an entire project.

Mohammed-Amine Rouh January 13, 2019

Hi, Did anyone figure it out for JIRA Cloud ?

Suggest an answer

Log in or Sign up to answer