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

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

This widget could not be displayed.

Hi Mel,

I  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")
def valB = getCustomFieldValue("B")

// 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

This widget could not be displayed.

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

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

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 ?

OPSCV is a calculated number field. Is it impacting ?

This widget could not be displayed.

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

This widget could not be displayed.

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

Oops. Chander beat me to it! Sorry!

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

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)

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

This widget could not be displayed.

thanks a lot Kristian - it perfeclty works now

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

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

 

 

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

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" 

}

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

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!

This widget could not be displayed.

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

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

This widget could not be displayed.

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

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

146 views 2 0
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you