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)
Both of these approaches are broadly right, but make small mistakes that are well worth explaining.
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.
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? :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
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.