Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner for JIRA Cloud - Calculate Custom Field Template Fails

Paul_Sorauer October 8, 2018

Hi there

I'm trying to use Adaptavist's sample code to calculate a custom field on Issue Update.

I'm using the code template as-is, only modifying the issue field names as per my issue type. but I seem to be getting a type conversion error:

2018-10-09 04:32:57.096 INFO - Serializing object into 'interface java.util.List'
2018-10-09 04:32:57.137 INFO - GET /rest/api/2/field asObject Request Duration: 875ms
2018-10-09 04:32:57.175 ERROR - Cannot coerce a map to class java.lang.Integer because it is a final class on line 17
2018-10-09 04:32:57.177 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null

I used the "Calculated Custom Field" link to insert the template on the add new listener page, but it's pretty much as per the following:

https://scriptrunner-docs.connect.adaptavist.com/jiracloud/script-listeners.html#_calculate_custom_field_on_issue_update

Here is my script:

// get custom fields
def customFields = get("/rest/api/2/field")
.asObject(List)
.body
.findAll { (it as Map).custom } as List<Map>

def input1CfId = customFields.find { it.name == 'Potential Impact' }?.id
def input2CfId = customFields.find { it.name == 'Likelihood' }?.id
def outputCfId = customFields.find { it.name == 'Severity' }?.id
def projectKey = "IMPT"

if (issue == null || (issue.fields.project).key != projectKey) {
logger.info("Wrong Project \${issue.fields.project.key}")
return
}


def input1 = issue.fields[input1CfId] as Integer
def input2 = issue.fields[input2CfId] as Integer

if (input1 == null || input2 == null) {
logger.info("Calculation using \${input1} and \${input2} was not possible")
return
}

def output = input1 * input2

if (output == (issue.fields[outputCfId] as Integer)) {
logger.info("already been updated")
return
}

put("/rest/api/2/issue/\${issue.key}")
.header("Content-Type", "application/json")
.body([
fields:[
(outputCfId): output
]
])
.asString()

 

Can anyone tell me what the issue is, please?

 

Cheers,

Paul

2 answers

1 accepted

1 vote
Answer accepted
Paul_Sorauer October 11, 2018

As per above, with some help from Adaptavist, I worked out that my custom field was a complex type:

"customfield_10103" : {
"self" : "https://XXXXXXXXXXXX.atlassian.net/rest/api/2/customFieldOption/10203",
"value" : "4",
"id" : "10203"
}


The answer then is pretty simple:

def input1 = issue.fields[input1CfId].value as Integer
0 votes
Tarun Sapra
Community Champion
October 9, 2018

Hello @Paul_Sorauer

You are facing this error because in the original code sample, this portion of the code

def input1 = issue.fields[input1CfId] as Integer
def input2 = issue.fields[input2CfId] as Integer

excepts the customFields input1CfId & input2CfId of type "Number" field that's why they are being "cast" into Integers.

In your case, what's the custom field type of the custom fields "Potential Impact" and "Likelihood" , if they are not number field then you would need to modify the code to match the type of the custom field.

Paul_Sorauer October 9, 2018

Hi @Tarun Sapra 

Thanks for the assist. I also got some help from Adaptavist and it turns out that my custom field was a complex type:

"customfield_10103" : {
"self" : "https://XXXXXXXXXXXX.atlassian.net/rest/api/2/customFieldOption/10203",
"value" : "4",
"id" : "10203"
}


The answer then is pretty simple:

def input1 = issue.fields[input1CfId].value as Integer


Having never used Groovy before I had no idea where even to begin.

Thanks either way,

Paul

Tarun Sapra
Community Champion
October 10, 2018

Glad to know @Paul_Sorauer that it's working for you, please accept/upvote the answer so that others are helped as well. thanks!

Sarthak Sharma March 9, 2021

Thanks Tarun :)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events