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.
Actually they are same.
Anyway, for sure you have to add null check
<!-- @@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 ));
So, any update? Does this code work?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
-->
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad to hear that. So please accept the answer for the further usages.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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) + ...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is your custom fields' (11309, and others) type?
And what does below statement return?
issue.get("customfield_11309")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
By the way, cf1 is not "customfield_11309"
CustomField cf1 = customFieldManager.getCustomFieldObject("customfield_11309")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.