Get cf values from subtasks, add, post to parent

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

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

Hi Jon,

Thanks works great! Thanks for your assistance!

Illiterati

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

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!

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

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.

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 2 hours ago in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

13 views 0 3
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