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

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

Drew Nedderman January 23, 2018

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

=A+B+C+(D/2)

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?

 

4 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Drew Nedderman January 23, 2018

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) {
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 Integer
def input4 = issue.fields[input4CfId] as Integer

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

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

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()
4 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2018

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.

Aasha November 18, 2021

@Nic Brough (Adaptavist)  - I have similar requirement,

 

For example,  there is a field A and B - whatever values given in A and B, it should be calculated in C field. Whenever these two fields are updated, How to achive this in script runner.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 18, 2021

Just modify the calculation given in the code in my answer.

Aasha November 19, 2021

@Nic Brough -Adaptavist-  -

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

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

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

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

is that correct?

Aasha December 6, 2021

@Nic Brough _Adaptavist_  - We can achieve by writing this code

 

import com.atlassian.jira.component.ComponentAccessor
 
def customFieldManager = ComponentAccessor.customFieldManager
 
def numberA = customFieldManager.getCustomFieldObjectsByName('Number A').first()
 
def numberB = customFieldManager.getCustomFieldObjectsByName('Number B').first()
 
def numberAValue = numberA.getValue(issue) as Double
 
def numberBValue = numberB.getValue(issue) as Double
 
if(numberAValue && numberBValue) {
    return (numberAValue + numberBValue) as Double
} else if(numberAValue && !numberBValue) {
    return (numberAValue + 0) as Double
} else if (!numberAValue && numberBValue) {
    return (0 + numberBValue) as Double
} else {
    return 0 as Double
}
Chasovskikh Kirill March 28, 2022

how to adapt this script for jira cloud? 

thank you

1 vote
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 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

 

Bsrkprasad May 13, 2020

hello @Nic Brough -Adaptavist-  i have similar requirement . i have four  single select fields and out put should be  sum of three fields + divided by 4th field. i have implemented below piece of code . but addition and division logic is throwing error. could you let me know whats the error with this code

 

logicwhich is throwing error :

 

Double field1 = (("userBusinessValueNum" + "timecriticalityNum" + "riskreductionopportunityenablementvalueNum") / "jobsizeNum")

return field1

 

entire piece of code :

 

 

String userBusinessValue = getCustomFieldValue("User-Business Value")
String timecriticality = getCustomFieldValue("Time Criticality")
String riskreductionopportunityenablementvalue = getCustomFieldValue("Risk Reduction-Opportunity Enablement Value")
String jobsize = getCustomFieldValue("Job Size")

String userBusinessValueNum = ""
String timecriticalityNum = ""
String riskreductionopportunityenablementvalueNum = ""
String jobsizeNum = ""


switch (userBusinessValue) {
case "1":
userBusinessValueNum = "1"
break
case "2":
userBusinessValueNum = "2"
break
case "3":
userBusinessValueNum = "3"
break
case "5":
userBusinessValueNum = "4"
break
case "8":
userBusinessValueNum = "5"
break
case "13":
userBusinessValueNum = "6"
break
case "20":
userBusinessValueNum = "7"
break

default:
userBusinessValueNum = "0"
}

switch (timecriticality) {
case "1":
timecriticalityNum = "1"
break
case "2":
timecriticalityNum = "2"
break
case "3":
timecriticalityNum = "3"
break
case "5":
timecriticalityNum = "4"
break
case "8":
timecriticalityNum = "5"
break
case "13":
timecriticalityNum = "6"
break
case "20":
timecriticalityNum = "7"
break
default:
timecriticalityNum = "0"
}

switch (riskreductionopportunityenablementvalue) {
case "1":
riskreductionopportunityenablementvalueNum = "1"
break
case "2":
riskreductionopportunityenablementvalueNum = "2"
break
case "3":
riskreductionopportunityenablementvalueNum = "3"
break
case "5":
riskreductionopportunityenablementvalueNum = "4"
break
case "8":
riskreductionopportunityenablementvalueNum = "5"
break
case "13":
riskreductionopportunityenablementvalueNum = "6"
break
case "20":
riskreductionopportunityenablementvalueNum = "7"
break
default:
riskreductionopportunityenablementvalueNum = "0"
}

switch (jobsize) {
case "1":
jobsizeNum = "1"
break
case "2":
jobsizeNum = "2"
break
case "3":
jobsizeNum = "3"
break
case "5":
jobsizeNum = "4"
break
case "8":
jobsizeNum = "5"
break
case "13":
jobsizeNum = "6"
break
case "20":
jobsizeNum = "7"
break
default:
jobsizeNum = "0"
}

Double field1 = (("userBusinessValueNum" + "timecriticalityNum" + "riskreductionopportunityenablementvalueNum") / "jobsizeNum")

return field1

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 14, 2020

Select lists hold option objects, which are a Jira class, not primitive or standard objects

So, the line

String userBusinessValue = getCustomFieldValue("User-Business Value")

will work as the options can be cast to strings, but the strings won't be as simple as the text of the label.

Try replacing that line with

def cfValue = getCustomFieldValue("User-Business Value")

def userBusinessValue = cfValue.getName()

Bsrkprasad May 19, 2020

@Nic Brough -Adaptavist- thanks for your reply.do you want me to replace like this below ?. how do i add the logic  if you can  suggest ?. and the switch case values i added will it remain or will it be also removed from script ?

 

def cfValue1 = getCustomFieldValue("User-Business Value")

def userBusinessValue = cfValue1.getName()

 

def cfValue2 = getCustomFieldValue("Time Criticality")

def timecriticality = cfValue2.getName()

 

def cfValue3 = getCustomFieldValue("Risk Reduction-Opportunity Enablement Value")

def riskreductionopportunityenablementvalue = cfValue3.getName()

 

 

def cfValue4 = getCustomFieldValue("Job Size")

def jobsize = cfValue4.getName()

Bsrkprasad May 20, 2020

@Nic Brough -Adaptavist- It would be helpful if you can throw some light on it .

Joel Batac May 21, 2022

@Bsrkprasad  - were you able to solve this? I have similar issue/need. I have 4 single select fields that need computation. 

0 votes
RMahankali November 26, 2022

Here is the base query to count the values of multiple custom fields... fine tune it per your needs:

def count = 0;
// first custom field, eg id 10400
def list = getCustomFieldValue(10400) as List;
count += list ? list.size() : 0
// second custom field, eg id 10402, copy this and the next line as often as you want to calculate the options of many fields.
list = getCustomFieldValue(10402) as List;
count += list ? list.size() : 0
list = getCustomFieldValue(10403) as List;
count += list ? list.size() : 0
list = getCustomFieldValue(10404) as List;
count += list ? list.size() : 0
count ? count : null

TAGS
AUG Leaders

Atlassian Community Events