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!
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.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have you tried return null instead of return?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It seems that the code worked well.
If the snippet is the custom field code, then returning null would clear the field's content.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.