how to calculate the sum of 2 custom fields via a groovy script field?

mel vega January 7, 2016

Hi,

I tried this script in a groovy script field :

FormField ITCost = getFieldByName ("IT Cost (T&M)")

FormField OPSCost = getFieldByName ("Total OPS Number of MD Time & Material")

def ITCV = ITCost.getFormValue() ?: 0

def OPSCV = OPSCost.getFormValue() ?: 0

def ITOPSVSUM = ITCV + OPSCV

but I got this error message :

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script5.groovy: 1: unable to resolve class FormField  @ line 1, column 11.   FormField ITCost = getFieldByName ("IT Cost (T&M)")             ^ Script5.groovy: 2: unable to resolve class FormField  @ line 2, column 11.   FormField OPSCost = getFieldByName ("Total OPS Number of MD Time & Material")             ^ 2 errors

Any ideas ?

Thanks in advance

7 answers

1 accepted

1 vote
Answer accepted
Kristian Walker _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 7, 2016

Hi Mel,

I can confirm that if  a value is null then it will not be able to be used in a calculation. 

You could use an if else statement to check if one of the values is null and only do the calculation if both fields contain values.

An example of what this might look like using Nic's code above is below.

// Get values from 2 custom fields
def valA =  getCustomFieldValue("A") as Double
def valB = getCustomFieldValue("B") as Double

// check both fields contain valid numeric values
if (valA != null && valB != null){
    return valA + valB
}else{
    // return to some code to indicate a null value in one of the fields
 return "-1"   
}

 

I hope this helps

Kristian

2 votes
Chander Inguva
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 7, 2016

Hi,

   Try this simple code in field C

def aval = getCustomFieldValue("A")
def bval = getCustomFieldValue("B")
return aval+bval

Assuming A,B,C are number fields

Chander Inguva
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 7, 2016

Use def only when you don't know the type, else use int/double accordingly

mel vega January 7, 2016

Hi thanks to you and phill. by Try this simple code in field C, you mean in the description or in Inline scripted field tab via Admin ?

mel vega January 7, 2016

OPSCV is a calculated number field. Is it impacting ?

1 vote
Phill Fox
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 7, 2016

Are you importing your classes in advance of this section of script? If not this could be the cause of your error.

Eg 

import com.atlassian.jira.component.ComponentAccessor

Chander Inguva
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 7, 2016

I agree with Phil.

0 votes
Heather Rimmey January 2, 2018

I'm trying to script a validation on summing just 2nd value from multiple cascading fields. Does anyone know if this is possible? 

cfValues["CascadingSelect 1"]?.values()*.value == ["AAA", "a1"]
cfValues["CascadingSelect 2"]?.values()*.value == ["BBB", "b1"]

 a1 + b1 == 100

0 votes
Jeff Tillett
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.
September 16, 2016

Is there a way to do this with time fields? I was able to use this same code, however I get a result of 0, and I'm thinking it's b/c my custom fields are time/date fields.

I am attempting to calculate the total duration between two custom date fields using:

import com.atlassian.jira.component.ComponentAccessor
return getCustomFieldValue("Outage End") - getCustomFieldValue("Outage Start")

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.
September 16, 2016

I don't think the fields will be returning objects you can perform arithmetic on. 

I seem to remember that they'll give you date/time stamps, which you could convert to "epoch" time, which is a number of (milli)seconds since a base date.  Then subtract them, and convert the resulting number of (milli)seconds back up to minutes/hours/years/etc

0 votes
mel vega January 7, 2016

thanks a lot Kristian - it perfeclty works now

Kristian Walker _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 8, 2016

Hi Mel, I am glad the solution works. Thank you for accepting the answer. Kristian

mel vega January 27, 2016

Hi @Kristian Walker (Adaptavist)

Sorry I get back to this request because finally some of my number fields can be null. Is there a solution to integrate a value= null into the formula? if not would you have an idea to be able to add several number fields, assuming that one or several of them can be null?

thanks in advance for your help

Melissa

 

 

Kristian Walker _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 27, 2016

You would just need to check in the if statement for if value == null. You will not be able to add a null value to a number. What you would need to do is have a separate if statement for each field before the calculation which adds the values up and if it any field is null set its value to 0 so that it can be used in the calculation.

I hope this helps

Kristian

mel vega January 27, 2016

thanks @Kristian Walker (Adaptavist). I tried this code but it does not work and I get a message telling me that the null values are not supported by this method. Do i have to write this stateent in the description of the field ?

import com.atlassian.jira.component.ComponentAccessor

def aval = getCustomFieldValue("Accounting Quality & Integrity Control Cost (T&M)") def bval = getCustomFieldValue("Bonds Cost (T&M)") def cval = getCustomFieldValue("Collateral/Billing Cost (T&M)") def dval = getCustomFieldValue("Corporate Actions Cost (T&M)") def eval = getCustomFieldValue("Equities Cost (T&M)") def fval = getCustomFieldValue("Primary Issuance Cost (T&M)") def gval = getCustomFieldValue("Reference Data Cost (T&M)") def hval = getCustomFieldValue("IT Cost (T&M)")

// check both fields contain valid numeric values

if (aval == null) return aval("0")

if (bval == null) return bval("0") if (cval == null) return cval("0") if (dval == null) return dval("0") if (eval == null) return eval("0") if (fval == null) return fval("0") if (gval == null) return gval("0") if (hval == null) return hval("0")

if (aval != null && bval != null && cval != null && dval != null && eval != null && fval != null && gval != null && hval != null){         return aval+bval+cval+dval+eval+fval+gval+hval

}else{

    // return to some code to indicate a null value in one of the fields

return "0" 

}

Kristian Walker _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 27, 2016

Hi Mel,

 

If you use something similar to the code below then it will set the value to 0 if the field is null. You can add multiple fields in by getting each field and adding an if block for each field to set it 0 if its null.

def aval = getCustomFieldValue("A")
def bval = getCustomFieldValue("B")

// check both fields contain valid numeric values and if not set them to 0

if (aval == null) {
    aval = 0 
}
if (bval == null) {
    bval = 0
}
if (aval != null && bval !=null){
    return aval + bval
}else{
    // return to some code to indicate a null value in one of the fields
    return "0"
}

I hope this helps.

Kristian

mel vega January 28, 2016

Hi Kristian,

it's great

! thanks you helped me a lot. It works.

sorry I don't have any programming skils so I don't know specific formula to build a code!

0 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 7, 2016

I think you're confusing a script with a behaviour.  Formfield is not a script object, you'd actually want to use custom field objects.  Or, as it's groovy, the easier option of just "def something = x". 

In fact, you should just enter

return getCustomFieldValue("A") + getCustomFieldValue("B")

in the scripted field and set the output type to number

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 7, 2016

Oops. Chander beat me to it! Sorry!

mel vega January 7, 2016

Thanks it works except if the value are null. how can I precise that please?

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 7, 2016

You pretty much had it in your question to be honest. But I'd do something like def returnval = 0 def myA = getCustomFieldValue("A") ?:0 def myB = getCustomFieldValue("B") ?:0 return (myA + myB)

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 7, 2016

Bother, scraped the wrong chunk of script. Lose the first line of that!

Suggest an answer

Log in or Sign up to answer