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?
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()
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just modify the calculation given in the code in my answer.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
how to adapt this script for jira cloud?
thank you
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Bsrkprasad - were you able to solve this? I have similar issue/need. I have 4 single select fields that need computation.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.