Forums

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

Clear calculated field if any operand is null Scriptrunner

Drew Nedderman
Contributor
March 2, 2021

I'm using Scriptrunner to populate a calculated field. If for some reason I need to clear any one of the fields used to make the calculation, I need the calculated field to become null. I'm using this code to calculate the field:

def input1CfId = 'customfield_12326'
def input2CfId = 'customfield_12328'
def input3CfId = 'customfield_12329'
def outputCfId = 'customfield_12330'
def projectKey = "CT"

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
def input3 = issue.fields[input3CfId] as java.lang.Float

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


def output = input1 * input2 / input3

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()


I tried adding this in line 21 but it didn't work:

if (input1 == null || input2 == null || input3 == null) { 
def output = '0'
}

Any thoughts about what I could be doing differently here are appreciated!

2 answers

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 2, 2021

Your line 21 won't work because the script never gets there - you're terminating it by doing the same test on line 16.  Try putting your output = 0 in there instead of the return.

Drew Nedderman
Contributor
March 3, 2021

@Nic Brough -Adaptavist-thanks for the help here, I'm now getting a different error in the logs after changing the code to this:

def input1CfId = 'customfield_12326'
def input2CfId = 'customfield_12328'
def input3CfId = 'customfield_12329'
def outputCfId = 'customfield_12330'
def projectKey = "CT"

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

def input1 = issue.fields[input1CfId] as java.lang.Float
def input2 = issue.fields[input2CfId] as java.lang.Float
def input3 = issue.fields[input3CfId] as java.lang.Float

if (input1 == null || input2 == null || input3 == null) {
logger.info("Calculation using ${input1}, ${input2}, and ${input3} was not possible")
def output = '0'
}


def output = input1 * input2 / input3

if (output == (issue.fields[outputCfId] as java.lang.Float)) {
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()

The error is:

CorrelationId: e04179ed-c172-494d-8415-158ea95bb19a
RUN Script identifier: 0c16371a-c617-4cc6-aec8-c2df4ebdbe85 com.adaptavist.sr.cloud.events.WebhookExecution (jira:issue_updated webhook fired for issue CT-371) 
Script name: Calculate RICE Score CT Took: 454ms Logs:
2021-03-03 14:24:59.118 INFO - Calculation using 1.0, null, and null was not possible
2021-03-03 14:24:59.198 ERROR - Ambiguous method overloading for method java.lang.Float#multiply.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
 [class java.lang.Character]
 [class java.lang.Number] on line 22
2021-03-03 14:24:59.277 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null

I'm not sure how to resolve this. Any ideas?

0 votes
Tuncay Senturk
Community Champion
March 2, 2021

Hi @Drew Nedderman 

Have you tried return null instead of return?

Drew Nedderman
Contributor
March 2, 2021

Hi @Tuncay Senturk , when I change lines 16-19 of the code above to this:

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

I get this in the logs:

CorrelationId: d3c45b3a-4f71-497a-917f-b925711b12af
RUN Script identifier: 0c16371a-c617-4cc6-aec8-c2df4ebdbe85 com.adaptavist.sr.cloud.events.WebhookExecution (jira:issue_updated webhook fired for issue CT-371) 
Script name: Calculate RICE Score CT Took: 316ms Logs:
2021-03-02 20:50:16.260 INFO - Calculation using 2, null, and null was not possible

 Any thoughts?

Tuncay Senturk
Community Champion
March 2, 2021

It seems that the code worked well.

If the snippet is the custom field code, then returning null would clear the field's content. 

Suggest an answer

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

Atlassian Community Events