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:
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
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad to know @Paul_Sorauer that it's working for you, please accept/upvote the answer so that others are helped as well. thanks!
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.