Forums

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

Script runner isn't working to calculate a field when issue is updated

Nivine Alaeddine October 10, 2022

Hi everyone, 

I am using ScriptRunner on my Jira cloud on a postfunction. The script is to calculate 2 fields and represent the score on the output field. Below the script I used and still it is not showing the result. Is there anything I missed?

final projectKey = 'MEG'

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

//Define the custom fields

def input1CfId = 'customfield.10068'
def input2CfId = 'customfield.10069'
def outputCfId = 'customfield.10070'

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}")
//.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
(outputCfId): output
]
])
.asString()

1 answer

0 votes
Kristian Walker _Adaptavist_
Community Champion
October 11, 2022

Hi Nivine,

Looking at your script it looks like it not finding the custom fields as they have not been specified correctly as custom fields in jira are usually returned in a format similar to customfield_1234 .

I would advise basing your script on the example in the docs page below:

In this example you will notice on lines 7 8 and 8 the fields are specified by name and then the code looks up these fields and returns the ids in the correct format so you do not need to worry about specifying the fields by ID in the correct format.

I hope this helps.

Regards,

Kristian

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events