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