Calculated number field

Nandeesh M S December 15, 2015

Hi,

I am trying to add two custom fields and i am using below script,

<!-- @@Formula:

(issue.get("customfield_11907") != null ? issue.get("customfield_11907") : 0) +

(issue.get("customfield_11912") != null ? issue.get("customfield_11912") : 0)

 -->

Is there anything wrong in this?

Because the values in the fields are displaying in the screen but it is not adding.

For example:

customfield_11907 has value : 100

customfield_11912 has value : 40

addition of these fields : 10,040 (i am getting answers like this)

expected answer is 140

Can any1 please help.

 

Thanks,

Nandeesh

2 answers

1 accepted

1 vote
Answer accepted
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 15, 2015

Hi Nandees,

 

here your code :

 

import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.customfields.option.Option;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
MutableIssue issue = issue;
CustomField cf1 = customFieldManager.getCustomFieldObject('customfield_11907');
CustomField cf2 = customFieldManager.getCustomFieldObject('customfield_11912');
Double value1 = issue.getCustomFieldValue(cf1) != null ? new Double(issue.getCustomFieldValue(cf1).toString()): new Double(0);
Double value2 = issue.getCustomFieldValue(cf2) != null ? (Double)issue.getCustomFieldValue(cf2): new Double(0);
return value1+value2;

Hope this helps,

Fabio

Nandeesh M S December 15, 2015

Hi Fabio, Great!! It worked. Thank you so much your help :) Thanks, Nandeesh

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 15, 2015

You're welcome :)

0 votes
Phill Fox
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 15, 2015

Hi Nandeesh,

The reason for your getting 10,040 rather than 140 is that your initial values are being treated as strings and so when you use the + operation it concatenates the two values together giving you 10040 but then this is being displayed as a number.

See https://innovalog.atlassian.net/wiki/display/KB/Using+issue.get(%3Cfield_name%3E)+in+scripts for a more comprehensive explanation as to why.

The solution is to ensure that you treat the value returned in the formula as numbers not a string then the + operation will work fine.

Eg 

(issue.get("customfield_11907") != null ? (Integer.parseInt((String)issue.get("customfield_11907")) : 0) 

please note this is from memory and not tested.

 

Hope this helps 

Phill

Nandeesh M S December 15, 2015

Hi Phill, Its not working!!When i try with your example, its not showing any value. <!-- @@Formula: (issue.get("customfield_11907") != null ? (Integer.parseInt((String)issue.get("customfield_11907")) : 0) + (issue.get("customfield_11912") != null ? (Integer.parseInt((String)issue.get("customfield_11912")) : 0) -->

Phill Fox
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 15, 2015

HI Nandeesh Can you confirm what custom field type your two fields (11907 and 11912) are. You may also like to try this format <!-- @@Formula: (issue.get("customfield_11907") != null ? (Integer.parseInt(issue.get("customfield_11907")) : 0) + (issue.get("customfield_11912") != null ? (Integer.parseInt(issue.get("customfield_11912")) : 0) -->

Nandeesh M S December 15, 2015

Hi Phill, No luck.. it dint work. 11907 - select list 11912 - number field Thanks, Nandeesh

Suggest an answer

Log in or Sign up to answer