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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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?

 

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

 

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 .

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

Share your Confluence Cloud experience for a chance to win $500!

Take our 5-minute survey to win a $500 Visa gift card! Are you currently using Confluence Cloud? We want to hear from you! Fill out this quick survey about your Confluence Cloud experience so we ...

260 views 5 13
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you