Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.