Can Scriptrunner calculate the number value of a custom field based on other custom fields? Edited

I am trying to find a way to do this calculation in a custom field:


The four variables above are entered in their own custom fields, then a fifth custom field contains the formula to calculate the total. Can this be done with Scriptrunner? 


I neglected to mention that I need this to work in cloud, not server. Is this still possible?


3 answers

1 accepted

0 votes
Accepted answer

Thanks everyone, I used this to get my custom field value to calculate. I appreciate the help!

def input1CfId = 'customfield_12326'
def input2CfId = 'customfield_12327'
def input3CfId = 'customfield_12328'
def input4CfId = 'customfield_12329'
def outputCfId = 'customfield_12330'
def projectKey = "MS"

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

def input1 = issue.fields[input1CfId] as Integer
def input2 = issue.fields[input2CfId] as Integer
def input3 = issue.fields[input3CfId] as Integer
def input4 = issue.fields[input4CfId] as Integer

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

def output = input1 + input2 + input3 + (input4/2)

if (output == (issue.fields[outputCfId] as Integer)) {"already been updated")

//.queryString("overrideScreenSecurity", Boolean.TRUE)
.header("Content-Type", "application/json")
(outputCfId): output
2 votes

Absolutely.  If you do this in a "scripted field",

def A = issue.getCustomFieldValue ( customFieldManager.getCustomFieldObjectByName("A") )

is the basics of getting the content.  Repeat for the others, then

return (double) (A + B + C + (D/2))

Set the "template" to numeric and the searcher to a number range.

0 votes

It can be done. You have to create 4 number custom fields and a scripted field with a script like this

import com.atlassian.jira.component.ComponentAccessor

def csA = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("A")

def csB = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("B")

def csC = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("C")

def csD = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("D")

 return issue.getCustomFieldValue(csA) + issue.getCustomFieldValue(csB) + issue.getCustomFieldValue(csC) + issue.getCustomFieldValue(csD)/2


Suggest an answer

Log in or Sign up to answer
Community showcase
Published Wednesday in Jira

Make your Atlassian Cloud products more secure: our NEW admin security guide

Hey admins! I’m Dave, Principal Product Manager here at Atlassian working on our cloud platform and security products. Cloud security is a moving target. As you adopt more products, employees consta...

100 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you