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

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

mel vega Jan 07, 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 Community Leader Jan 07, 2016

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

1 vote
chander inguva Jan 07, 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 Jan 07, 2016

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

mel vega Jan 07, 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 Jan 07, 2016

OPSCV is a calculated number field. Is it impacting ?

0 votes
Phill Fox Jan 07, 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 Jan 07, 2016

I agree with Phil.

0 votes
Nic Brough [Adaptavist] Community Leader Jan 07, 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 Jan 07, 2016

Oops. Chander beat me to it! Sorry!

mel vega Jan 07, 2016

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

Nic Brough [Adaptavist] Community Leader Jan 07, 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 Jan 07, 2016

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

0 votes
mel vega Jan 07, 2016

thanks a lot Kristian - it perfeclty works now

Kristian Walker Community Leader Jan 08, 2016

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

mel vega Jan 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 Community Leader Jan 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 Jan 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 Community Leader Jan 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 Jan 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
Jeff Tillett Community Leader Sep 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 Sep 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
Heather Rimmey Jan 02, 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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

12,629 views 63 59
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