Scriptrunner Scripted field to sum fields of child tasks

Wade Hephner August 30, 2018

Hello all, I have bee researching this and have not been able to find the answer.  I have a scripted field on a parent task and I would like to sum a field of all child tasks with a specific link type. I have gotten this far with my script but I cannot figure out how to return the value to populate the parent task field. Here is my script so far - the bold area is what i am pretty sure is incorrect to be able to return the results of the jql query.

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter


def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// jql query to sum fields
def query = jqlQueryParser.parseQuery("issueFunction in hasLinks('is task of') AND issueFunction in aggregateExpression('Total Story Points', 'customfield_10209.sum()')")
//get results
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

return results

 

Thanks in advnace,

Wade 

3 answers

1 accepted

1 vote
Answer accepted
Mauricio Karas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 3, 2018

Hello, Wade. Thanks for reaching out to the Atlassian Community!

I was able to achieve what you need with the following script without making JQL queries:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.SubTaskManager

SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();
Collection subTasks = issue.getSubTaskObjects();

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def customField = customFieldManager.getCustomFieldObjectByName("<Custom Field Name>");
def sum = 0;

for (currIssue in subTasks)
if (currIssue.getIssueType().name == "<Subtask Issue Type>")
sum += currIssue.getCustomFieldValue(customField) as int;

return sum

Just replace the "<Custom Field Name>" and "<Subtask Issue Type>" with the correct information and try the script.

Kind regards,
Maurício Karas

Wade Hephner September 4, 2018

Thanks Mauricio Karas, this looks promising, however I am not selecting child tasks my issue type but by link type -- issueFunction in hasLinks('is task of') -- could that be plugged into the if statement?

Mauricio Karas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 4, 2018

Yes, Wade. My bad, I misunderstood the question, I thought you were trying to get subtasks, but it's actually linked issues.

I was able to create the following script to get the linked issues:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink;

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def customField = customFieldManager.getCustomFieldObjectByName("<Custom Field Name>");
def sum = 0;

List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();

if(issueLink.getIssueLinkType().name == "Blocks"){
def linkedIssue = issueLink.getDestinationObject();
log.error(linkedIssue.getCustomFieldValue(customField));
sum += linkedIssue.getCustomFieldValue(customField) as int;
}
}

return sum

 In the "if" statement you need to compare with the outward link type, in my case "Blocks".

Let me know if this works for you.

Kind regards,
Maurício Karas

Wade Hephner September 4, 2018

Mauricio Karas brilliant. works like a charm. thanks so much for the help.

1 vote
Mark Markov
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.
September 4, 2018

Hello @Wade Hephner

In case if this is subtasks, it is better to do without jql as @Mauricio Karas says

If issues links, you can do it like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter


def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObjectsByName("FieldName").first()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// jql query to sum fields
def query = jqlQueryParser.parseQuery("worklogAuthor = admin")
//get results
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
def issues = results.getIssues()
def result = issues.collect {it.getCustomFieldValue(customField)}.sum()
return result

Just replace "FieldName" with field name that you want to sum

Wade Hephner September 4, 2018

Hello Mark Markov, thanks for the reply. When I put this in I get null as the result. I have replaced "FieldName" with the field in the child task that I am trying to sum which right now should be 7.

0 votes
Nidhi Gupta
Contributor
May 24, 2023

Hello All, I used the code mentioned in the accepted answers to sum up the values from a custom field on the sub-tasks of type select list (single select). The aggregate will show up on the parent issue under a scripted field but I am getting following errors. Would appreciate any help to resolve this.

Code2.jpg

Code1.jpg

Suggest an answer

Log in or Sign up to answer