Forums

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

JIRA Calculated number sum

Joe Harmon
Contributor
May 23, 2018

I am using the JIRA Calculated field and it isn't giving me the correct results.  I have 5 custom fields.  These fields have values between 0 -3 which can be selected using the single select custom field.  I am then trying to all all of these together to get a number.  My formula is below. 

<!-- @@Formula:
issue.get("customfield_11309")+issue.get("customfield_11310")+issue.get("customfield_11311")+issue.get("customfield_11312")+issue.get("customfield_11313")
-->

Let's say for the sake of simplicity that I am selecting the number 1 for each of these.  I would expect it to return the value of 5. However, it is instead putting the numbers side by side and I am getting the value of 11,111

Any help would be great.

3 answers

1 accepted

1 vote
Answer accepted
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018

Actually they are same.

Anyway, for sure you have to add null check

&lt;!-- @@Formula:

Object cf1Val = issue.get("customfield_11309");
Object cf2Val = issue.get("customfield_11310");
Object cf3Val = issue.get("customfield_11311");
Object cf4Val = issue.get("customfield_11312");
Object cf5Val = issue.get("customfield_11313");
int sum = ((cf1Val != null ? Integer.parseInt(cf1Val.toString()) : 0 )) 
+ ((cf2Val != null ? Integer.parseInt(cf2Val.toString()) : 0 ))
+ ((cf3Val != null ? Integer.parseInt(cf3Val.toString()) : 0 ))
+ ((cf4Val != null ? Integer.parseInt(cf4Val.toString()) : 0 ))
+ ((cf5Val != null ? Integer.parseInt(cf5Val.toString()) : 0 ));

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018

So, any update? Does this code work?

Joe Harmon
Contributor
May 24, 2018

No for some reason it doesn't work.  I copied it in just as you have it.  Is there a way to debug it?  Does it output to any specific logs?

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 24, 2018

It should definitely work.

Did you return the value.

It should be as below

<!-- @@Formula:

Object cf1Val = issue.get("customfield_11309"); Object cf2Val = issue.get("customfield_11310"); Object cf3Val = issue.get("customfield_11311");
Object cf4Val = issue.get("customfield_11312");
Object cf5Val = issue.get("customfield_11313");

int sum = ((cf1Val != null ? Integer.parseInt(cf1Val.toString()) : 0 ))
+ ((cf2Val != null ? Integer.parseInt(cf2Val.toString()) : 0 ))
+ ((cf3Val != null ? Integer.parseInt(cf3Val.toString()) : 0 ))
+ ((cf4Val != null ? Integer.parseInt(cf4Val.toString()) : 0 ))
+ ((cf5Val != null ? Integer.parseInt(cf5Val.toString()) : 0 ));

return sum;
-->
Like • Jen Adel likes this
Joe Harmon
Contributor
May 24, 2018

It's working.  Thanks for all your help.

0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 24, 2018

Glad to hear that. So please accept the answer for the further usages.

Joe Harmon
Contributor
May 24, 2018

done

0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018

Hello,

You're getting 11111 because you're concating five 1

You should cast the result to integer before adding.

Integer.parseInt(...) + Integer.parseInt(...) + ...

 or 

(Integer)issue.getCustomFieldValue(cf1) + (Integer)issue.getCustomFieldValue(cf2) + ...
Joe Harmon
Contributor
May 23, 2018

Thanks for the response.  For some reason, (Interger)issue.getCustomFieldValue("customfield_11313") isn't returning a value for any of the plugged in custom fields.

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018 edited

What is your custom fields' (11309, and others) type?

And what does below statement return?

issue.get("customfield_11309")

 

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018

By the way, cf1 is not "customfield_11309"

CustomField cf1 = customFieldManager.getCustomFieldObject("customfield_11309")
Joe Harmon
Contributor
May 23, 2018 edited

All 5 custom fields are "Select List (single choice)" custom files.  I then have in the list the values of 0, 1, 2, 3.  But I suspect that it is being seen as text and not a numeric value.

issue.get("customfield_11309") returns the current set value of that field (currently set to 2)

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018

OK, then try this one

int cf1Val = Integer.parseInt(issue.get("customfield_11309"))
int cf2Val = Integer.parseInt(issue.get("customfield_11310"))
int cf3Val = Integer.parseInt(issue.get("customfield_11311"))
int cf4Val = Integer.parseInt(issue.get("customfield_11312"))
int cf5Val = Integer.parseInt(issue.get("customfield_11313"))

int total = cf1Val + cf2val + cf3Val + cf4Val + cf5Val
Joe Harmon
Contributor
May 23, 2018

Nope, that one didn't work, but this one does if all the fields are set:

<!-- @@Formula:
Integer.parseInt(issue.get("customfield_11309"))+
Integer.parseInt(issue.get("customfield_11310"))+
Integer.parseInt(issue.get("customfield_11311"))+
Integer.parseInt(issue.get("customfield_11312"))+
Integer.parseInt(issue.get("customfield_11313"))
-->

If any of the fields are set to none or not set, then it doesn't work.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events