Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

sum up a custom field value of all sub-task for a particular parent issue type

Priti Sonar April 27, 2022

Hello ,

I wanted to sum custom field values of all subtask for respective parent issue type .

Case :

Parent issue Type have 3 subtask .

In all sub-task there is custom field name "A".

I wanted to make a new field in parent issue type that calculate sum of A of all respective sub-task .

1st subtask -A=30

2nd subtask A=30

3rd subtask A=20.

Parent  ticket will have "new_field"=30+30+20.

Could anyone help me to achieve through which scriptrunner and groovy script .

Please let me know  if I fail somewhere to clear my requirements

1 answer

1 vote
Radek Dostál
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 27, 2022
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField

final long CUSTOMFIELD_ID = 41610L

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField numberCustomField = customFieldManager.getCustomFieldObject(CUSTOMFIELD_ID)
if (numberCustomField == null) {
log.error(customField.getName() + " (Script Field) unable to run! The provided field under id '" + CUSTOMFIELD_ID + "' was not found!")
return null
}

Double aggregatedSubtasksValue = 0D

for (Issue subtaskIssue : issue.getSubTaskObjects()) {
Double subtaskValue = (Double) subtaskIssue.getCustomFieldValue(numberCustomField)

//Since we are aggregating multiple values, let's replace nulls with 0 instead
if (subtaskValue == null)
subtaskValue == 0D

aggregatedSubtasksValue += subtaskValue
}

return aggregatedSubtasksValue

With 'Number Field' template works locally.

 

That is, assuming that your custom field on subtasks is actually a number field. If not, then of course Double type will not work - Double is specifically only Number Field.

The snippet is more java than groovy - hence the lack of "def" variable types and so forth. Normally I would advise everyone to start with groovy at first, because it is a lot easier to understand and write until you get a better sense of Atlassian API and which classes to expect and where - but I just can't do it anymore and my IDE won't work with groovy that well.

It doesn't ultimately matter though.

 

All that said, if you haven't, you really should try to write it on your own first - there are plenty of examples and similar threads, and Adaptavist have a pretty nice library with plenty of existing (and verified) codes: https://library.adaptavist.com/

Sure you might stumble and have to test a lot of stuff, but you'll know how everything works a lot better for future maintenance.

Suggest an answer

Log in or Sign up to answer