Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate the value of two single select custom fields (that have numeric values) using groovy scrip

Peter Brown June 24, 2019

I am trying to use a groovy script to calculate the value of one single select numeric custom field multiplied by another single select numeric custom field and im totally stuck.. any help?

 

def Impact = getCustomFieldValue(“Impact”)

def Probability = getCustomFieldValue(“Probability”)

 

if (Impact) {

    return Impact * Probability

}

else {

    return null

}

2 answers

1 accepted

1 vote
Answer accepted
Milan Chheda [INFOSYSTA]
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.
June 24, 2019

You can try using the below code. Firstly, I am getting the custom field by name, followed by getting the values as Double. If both the values are > 0, only then perform multiplication.

 

import com.atlassian.jira.component.ComponentAccessor

def customfieldManager = ComponentAccessor.getCustomFieldManager()

def impact = customfieldManager.getCustomFieldObjectByName("Impact");
def probability = customfieldManager.getCustomFieldObjectByName("Probability");

def impactValue = issue.getCustomFieldValue(impact) as Double
def probablityValue = issue.getCustomFieldValue(probability) as Double

if(impactValue > 0 && probablityValue > 0) {
return impactValue * probablityValue;
} else {
return null;
}


Peter Brown June 25, 2019

Thanks for this, much appreciated!

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 25, 2019

You actually didn't specify which app you are using to run this script. ScriptRunner? JMWE? Another app? It actually makes a difference.

Peter Brown June 25, 2019

Hi David, it was ScriptRunner!

I've fixed the issue i was having, thanks to everyone for their support.

0 votes
Vimalraj
Contributor
June 24, 2019

Hi Peter,

I have used SIL to perform calculations. Please refer below example. You may try below link for groovy

number planneda = argv["Planned_A"];
number plannedb = argv["Planned_B"];
number plannedtotal = pmplanned*seplanned;
lfSet("Total Planned",plannedtotal );

http://myagileplm.com/question/groovy-script-to-multiply-two-numeric/

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events