Forums

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

calculate sum from custom fields

Deleted user July 2, 2018

I'm new to Jira and I'm trying to calculate the effort and the costs of an issue based on different values for the effort in the issue. It is no problem to enter the different values for the effort in the issue and display them, but I am not able to display the result of my calculation.

I created Script Field and set the template to "Text Field (Multi Line)". I added the new field to the screen where I want it to be shown. When I use the preview feature for my example issue the preview just shows nothing. I don't get an error either. When I use the Fieldhelper it says, that there is no value set and therefore the field is not displayed. But I don't know why the value is not set, as the code seems to be working fine (no error).

Maybe somebody can point me to where I am wrong?

Here is some example code of what I'm doing:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
com.atlassian.jira.issue.MutableIssue;

IssueManager issueManager = ComponentManager.getInstance().getIssueManager()
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager()

CustomField totalField = customFieldManager.getCustomFieldObjectByName("Total")

def a = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Effort A"))?:0
def b = issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Effort B"))?:0

Object total = (a * 1500) + (b * 1000)

issue.setCustomFieldValue(totalField, total)

 I also tried a different approach and used a number field. The result is the same, there is no error and the field value is not set.

import com.atlassian.jira.ComponentManager
cManager = ComponentManager.getInstance().getCustomFieldManager()
int A = cManager.getCustomFieldObjectByName('Effort A')?.getValue()?:0
int B = cManager.getCustomFieldObjectByName('Effort B')?.getValue()?:0
int C = cManager.getCustomFieldObjectByName('Effort C')?.getValue()?:0
int D = cManager.getCustomFieldObjectByName('Effort D')?.getValue()?:0
int E = cManager.getCustomFieldObjectByName('Effort E')?.getValue()?:0
int F = cManager.getCustomFieldObjectByName('Effort F')?.getValue()?:0
return (A*1500) + (B*1000) + (C*1000) + (D*1300) + (E*375) + (F*1000)  

 

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 3, 2018

Both of these approaches are broadly right, but make small mistakes that are well worth explaining.

  • In the first script, you are getting the scripted field and trying to set it.  This fails because you don't need to do it.  The script is for generating the value the field will take, so all you do is "return (value)" like you do in your second script.
  • In both scripts, you are assuming that the fields exist and have data.  That could be true if you've set them mandatory, but if you have not, then "get field value" will not return a 0, it will return "null", which then makes your calculations fail.
  • There's also one last possible error, but I suspect you won't run into it - similar to the "empty" problem, but if the field is of the wrong type, it will simply hand an object into the variable.  My guess is you won't have hit this one, because I suspect you have set the fields as numbers.  In the first script, a and b will get the field values fine, but if either of them is not a number, the calculation will fail.  In the second script, it will try, and fail, to convert anything that isn't a string to an integer.  In fact, this could also fail because number fields are floating point, not integers.

So, you are very close, but have been tripped up by small details.

When you're fixing those things, bear in mind that the scripted field entry has a trick to help you a bit - go to the script and look at the bottom of the screen for "preview issue" - give it an issue you expect it to work for and it will run the script as a test.  The result will be the value as if the scripted field was calculated (don't worry, it's not touched your data), and there is a "log" tab for seeing what the server did and what any errors were.

Deleted user July 5, 2018

Hi and thank you for your answer.

I think I should explain my scenario a little bit more as I don't get my mistake yet.

The fields A to F are all number fields. The fields are configured to be shown in my screen. In a transition from one status to another status I set the values for these fields and they are shown as expected afterwards. My sum fields is under the fields A to F and my assumption was, that the field will be calculated when the data of the fields A to F is set. But the fields value is still empty.

I used the "preview issue" function and this returned "nothing", which means there was only a busy icon and then nothing was returned. If this means, that the field is still empty after the calculation I would suggest some kind of feedback in this case.

And what do you mean by "log" tab? :)

Deleted user July 5, 2018

I changed my code and had a look at the API doc. Your hint " In fact, this could also fail because number fields are floating point, not integers." pointed me to my error. 

First of all, I used the deprecated method "getCustomFieldObjectByName" instead of "getCustomFieldObjectsByName". I corrected that and this brought me to my next error, the missing import statement for the CustomField class. After I imported this, the script resulted in an error which said, that the expected return value should be a string and not a double. I corrected my script to the following, which now works fine. This 

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import java.text.NumberFormat

cManager = ComponentManager.getInstance().getCustomFieldManager()
ArrayList<CustomField> temp = null

temp = cManager.getCustomFieldObjectsByName('Effort A')
double a = temp?.get(0)?.getValue()?:0

temp = cManager.getCustomFieldObjectsByName('Effort B')
double b = temp?.get(0)?.getValue()?:0

def sum = (a*1500) + (b*1400)
return NumberFormat.getCurrencyInstance(Locale.GERMANY).format(sum)

 Thank you for your help!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events