Sum of values from subtasks using JIRA Calculated Number Field

Michael Danielsson
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.
November 7, 2013

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

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

6 votes
Answer accepted
Renjith Pillai
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.
November 10, 2013

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";

Michael Danielsson
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.
December 5, 2013

It works exept that sometimes when refreshing the Task it gets old values from some of the subtasks.

Any idea what can be wrong?

RenjithA December 10, 2013

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?

Darren Pegg
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.
October 23, 2014

This doesn't work at all for me... I'm assuming something has changed in the past year..

E.L. Fridge
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.
November 12, 2014

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.

AlicjaC January 26, 2015

Totally works, thank you!

Paweł November 13, 2015

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!

TAGS
AUG Leaders

Atlassian Community Events