Absorbing Field Content from A Subtask

Recently we have installed the "Jira Misc Custom Fields" extension. What we're trying to do is use the calculated field to display the values of the SUM of a sub-tasks's custom field.

For example:
Subtask A provides a value of 200 in a numeric field (cf 11212)
Subtask B provides a value of 150 in the same type of numeric field (cf 11212)

We want A + B to sum up and dump into the Calculated Field on the parent issue (we'll say this is C).

I have found one other Answer related to this, here
When I try to modify this to me it looks like this:

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


When I enter this into the Calculated Field, it isn't pulling any information. Even when using the "Where's my Field?" tool, this is saying it doesn't have any data.


I'm not the most knowledgable on JavaScript, but form the documentation and comments I believe this should be functioning. Anyone want to help point out what I'm doing wrong? I have figured out how to do this inside of the task itself, but when trying to take from external issues is the problem.

1 answer

1 accepted

This widget could not be displayed.

The source code is fine, but it seems to me you've choosen the wrong tool.

Please refer to https://jamieechlin.atlassian.net/wiki/display/GRV/Scripted+Fields from the script runner plugin. As for Misc Extention - I suppose there is little bit different syntax required and it is not so well-documented as script runner

Thanks for the reply. I've mainly used ScriptRunner when I wanted to fire off one-off jobs. Would I be able to write this script to somehow trigger itself each time an issue was updated, created, or on a schedule? (similar to a Cron setup)

Also please note that inside that code there is no null pointer exception check - value can be null if there is no value for the field inside subtask

Steven Behnke Community Champion Aug 14, 2014

You can run script runner as a service or write a listener that can kick off on events.

https://answers.atlassian.com/questions/115467/using-script-runner-as-a-service

Also if you select the option with calculated field - it will be refreshed each time the issue is viewed.

If you want a static readonly field that is being updated only when child issues are updated - your choise is the script listener, service is not better solution for that

https://jamieechlin.atlassian.net/wiki/display/GRV/Listeners

here is more info about creating listeners with script runner

Thanks for the info Alexey & Steve, I'm now putting my efforts towards configuring this on Script Runner via listener.

However, after looking at that bit of code a bit more I'm wondering if I need to point a few more things to actual fields and Issue Types.

For example, do I need to configure Line 16's "final Collection<issue>" and point it to a specific issue ID? From what I gather, this should be the field that the custom field (11212) is printing to?

If you are writing listener - first of all you should check that the issue that was updated is the needed one - so you should check that the issue is subtask and (maybe) lies in the needed project. After that you should check the changeset - if the field of your interest was changed. And after that - you are getting parent issue from your subtask - and then execute the code posted below - with the parent issue as issue.

Thanks for the suggestion, I ended up slightly modifying the script to work in a Scripted field:

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_11212");
           
        if(customFieldObject == null){
            return "Invalid Custom Field";
        }
        double total = 0;
        if (issue != null) {
            final Collection&lt;Issue&gt; subTaskObjects = issue.getSubTaskObjects();
            if(subTaskObjects != null &amp;&amp; subTaskObjects.size() != 0){
            String debug = "";
                for (Issue subTaskObject : subTaskObjects) {
                    final Object value = customFieldObject.getValue(subTaskObject);
                    total += Double.parseDouble(value.toString());
                    debug = debug + value.toString() + ", ";
                }
                return String.format("%.2f%n",total);
            }
            else{
                return "No subtasks";
            }
        }
        return "Internal Error";

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

510 views 3 18
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you