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

Next challenges

Recent achievements

  • Global
  • Personal


  • Give kudos
  • Received
  • Given


  • 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

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

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

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


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.

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

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).

0 votes
Tanya Gordon Community Leader Apr 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.


Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira Software

Presenting the "Best of 2020" Jira Software roundup!

Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...

4,137 views 5 15
Join discussion

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