Aggregating (Adding) up sub-tasks to a custom field. (Script Runner Help)

Sed Masic April 17, 2019

I've created a custom-field titled, "Capacity" 

I'm trying to add up all sub-tasks with a value in "Capacity" on a user story into a custom field titled on issue-type story entitled -  (Total Capacity). 

 

The below code is what I've tried and I haven't had much luck.

Any assistance would be appreciated!

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue

int totalSum = 0
for(Issue subtask: issue.getSubTaskObjects()){
CustomField Capacity = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Capacity")
if(subtask.getCustomFieldValue(Capacity) != null) {
totalSum += subtask.getCustomFieldValue(Capacity).toString().toInteger()
}
}

//return totalSum

2 answers

0 votes
Tanya Gordon
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.
April 18, 2019

The thing is that if you only need to display the capacity field then it's ok to use scripted field for that. But if you actually want to store this valie and to be able to use it in reports or dashboard you should use a regular number type field and set it's value from a scripted listener or escalation service.

Tanya

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 17, 2019

I like doing things the groovy way ... I think this should work:

import com.atlassian.jira.component.ComponentAccessor

def capacity = ComponentAccessor.customfieldManager.getCustomFieldObjectByName('Capacity')
def totalSum = issue.subTaskObjects.sum{
   it.getCustomFieldValue(capacity)?.toInteger() ?: 0
} ?: 0

totalSum

The magic is in the "sum" method  and the elvis operator ?:

The sum closure iterates over the subtasks and returns the capacity value or 0 if null.

The last elvis will return 0 in cases where there are no subtasks.

Sed Masic April 18, 2019

JiraSR.PNG

Sed Masic April 18, 2019

Attempted to change out the values as you suggested, and received the above error. 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2019

Oops, my bad.

Made a small error when transferring and adapting my example code from my environment.

Response above updated with

def capacity = ComponentAccessor.customfieldManager.getCustomFieldObjectByName('Capacity')

Bu what Tanya said is also true. This won't work if you try to report on this value since scripted field values are not actually stored in the database. They get calculated when you view the issue and when the issue is indexed (calculated value is available for searching, but won't get updated in the index when subtasks are modified).

Suggest an answer

Log in or Sign up to answer