Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner Scripted field to sum fields of child tasks

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 

2 answers

1 accepted

1 vote
Answer accepted

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

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?

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

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

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

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.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you