Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scripted Field Help

Aaron Geister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 28, 2022

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



Just looking for some guidance as I learn.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Aaron Geister
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 29, 2022

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. 

TAGS
AUG Leaders

Atlassian Community Events