Summing up in scripted field fails when zero is added

Hi,

I have the below scripted field to sum up estimates. It works fine mostly until one of the estimates contain a '0' at which point the script fails to display anything.

 

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField
CustomFieldManager cfManager = componentManager.getCustomFieldManager()
 def product_mangament_estimate cfManager.getCustomFieldObject(27922)?.getValue(issue); 
 def estimate_1 =  cfManager.getCustomFieldObject(13524)?.getValue(issue);
 def estimate_2 = cfManager.getCustomFieldObject(19222)?.getValue(issue);
 def estimate_3 = cfManager.getCustomFieldObject(34032)?.getValue(issue);
 def estimate_4 = cfManager.getCustomFieldObject(34033)?.getValue(issue);
 
if    ( (estimate_1) && (estimate_2) && (estimate_3) && (estimate_4) && (estimate_5))
return  ((estimate_1 +  estimate_2 + estimate_3 + estimate_4 + estimate_5)).round(0)
else {
    return null
}

Any ideas?

 

Thanks,

Gaj

 

1 answer

1 accepted

Well - you have a condition which will go into the else if any of the values is 0. In the else clause you return null and nothing is shown. The following boolean expression:

(estimate_1) && (estimate_2) && (estimate_3) && (estimate_4) && (estimate_5)

will return false if any of the variables has a value of 0.

Thank you Peter. But I would have thought entering 0 will be treated as a value rather than null. How can I allow for 0 to be a valid value?

Zero is a valid value - the problem is in your expression - "if (integerVar)" returns false for 0. If you want to check that the value is not null or not numeric, then you need something else - check out this thread for possible solutions.

That makes sense. thank you.

google for "groovy truth" ... if you want to check for null use (est == null)

Thanks all. managed to get this working.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

86 views 0 5
Read article

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