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

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 vote
Alexey Matveev Community Champion Jan 23, 2018

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

103 views 0 2
Join discussion

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