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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,456,276
Community Members
 
Community Events
176
Community Groups

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:

=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

0 votes
Answer 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) {
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()
3 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.

@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.

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

@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?

@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
}

how to adapt this script for jira cloud? 

thank you

1 vote

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

 

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

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

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

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

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

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

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events