Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
Level
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner for JIRA Cloud - Calculate Custom Field Template Fails

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

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 Leader Oct 09, 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.

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 Leader Oct 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!

Thanks Tarun :)

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you