I want to make a scripted field so that two other fields multiply to a scripted field called 'Total'. I am new to ScriptRunner cloud and still trying to learn groovy. This is what I found and added my fields and changed it to multiply instead of addition!
// 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 == 'Quantity' }?.id
def input2CfId = customFields.find { it.name == 'Standard Cost' }?.id
def outputCfId = customFields.find { it.name == 'Total' }?.id
def projectKey = "FS"
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()
I found the issue. It seems that there were to many fields with the same name so the Scriptrunner couldn't parse the name of the field returning a null value. I made an edit to the name of the 1st field and now it is working correctly.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.