Forums

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

When using Script Runner to Calculate a Custom field (cloud), fields with values showing null

Angie Vonnahme August 16, 2019

When I define which custom fields to look at, I have a rule for:

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

 

That is showing up as the result of my script, regardless of the fact that neither field is null. Both are number fields, with numbers and only numbers populated.

2 answers

0 votes
Derek Fields _RightStar_
Community Champion
August 19, 2019

Try replacing (outputCfld) with 'Base Requested Amount:' in the put statement. I know that you are using the example that Adaptavist has in their documentation, but I suspect that the problem is that the REST call is failing silently

You might also log what the output value is to make sure that it is being calculated as expected.

0 votes
Derek Fields _RightStar_
Community Champion
August 18, 2019

What is the rest of the script that does the calculation? What you show here is missing a '}' 

Angie Vonnahme August 19, 2019

// 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 == 'Development Initiative 1 Est. Cost:' }?.id
def input2CfId = customFields.find { it.name == 'Development Initiative 2 Est. Cost:' }?.id
def outputCfId = customFields.find { it.name == 'Base Requested Amount:' }?.id
def projectKey = "AFI"

if (issue == null || ((Map)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()

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events