Hi
Is it possible to sum a customfield in all subtasks to an issue using Calculated Number Field?
How can it be done?
Is there some other way to do this?
Regards,
Michael Danielsson
Community moderators have prevented the ability to post new answers.
Assuming the customfield is of type number, you can use the below script in a scripted field in the parent task (remember to replace the customfield number (customfield_10012 ) to the one you are using):
import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.fields.CustomField; IssueManager issueManager = ComponentAccessor.getIssueManager(); CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager(); final CustomField customFieldObject = customFieldManager.getCustomFieldObject("customfield_10012"); if(customFieldObject == null){ return "Invalid Custom Field"; } double total = 0; if (issue != null) { final Collection<Issue> subTaskObjects = issue.getSubTaskObjects(); if(subTaskObjects != null && subTaskObjects.size() != 0){ for (Issue subTaskObject : subTaskObjects) { final Object value = customFieldObject.getValue(subTaskObject); total += Double.parseDouble(value.toString()); } return String.format("%.2f%n",total); } else{ return "No subtasks"; } } return "Internal Error";
It works exept that sometimes when refreshing the Task it gets old values from some of the subtasks.
Any idea what can be wrong?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Not sure, as it is expected to get the correct values as long as the indexes are fine. Are you also making changes to the sub-tasks using some scripts or direct database updates?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This doesn't work at all for me... I'm assuming something has changed in the past year..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Worked just fine for me using JIRA 6.3.8 and the script runner plugin. I made a new scripted field with the "Text Field (multi-line)" template. I have noticed an occasional old values glitch as reported by Michael, but as soon as I refresh the screen its fine. I've had the most luck creating subtasks directly from the issue. It's when you go back and edit values in existing subtasks that the problem shows up.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do I have to install something to making this script works? I have calculated field working but this script isn't working in my jira. Thanks for answer!
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.