Get custom field values for all subtasks using Groovy

I've added a custom field to top level issues called Percent Complete. 

I want the value for this field to be calculated based on the percent complete of all of its subtasks, taking each subtask's weighting into account. 

All subtasks also have the custom Percent Complete field (which is updated manually), and a weight field (a value between 0 and 1, also set manually which modifies the parent issue's percent complete). 

In the end, a top-level issue's Percent Complete is equal to all subtask's Percent Complete values added together, after being multiplied by their weight.

The pseudocode would be something like this:

float percentComplete = 0;

Collection subTasks = issue.getSubTaskObjects();

for(subtask in subTasks) {
float weight = subtask.getCustomFieldValue("Weight");
float subCompletePercent = subtask.getCustomFieldValue("Percent Complete");
percentComplete += subCompletePercent * weight;
}

issue.setCustomFieldValue("Percent Complete", percentComplete);


I'm trying to do this in a script field.

If someone could give me a hand making this work, I would really appreciate it.

Thanks!

1 answer

Hi Jake,

I have taken the liberty of writing a script that should fulfill your requirements:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField

// ::.. Get custom field ..::
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def weight = customFieldManager.getCustomFieldObjectByName("Weight")
def subCompletePercent = customFieldManager.getCustomFieldObjectByName("Percent Complete")

// ::.. Retrieve custom field value ..::
def float retrieveValue(Issue issue, CustomField customField) {
def value = issue.getCustomFieldValue(customField)
if (value == null) {
return 0.0
} else {
return value as float
}

}

// ::.. Make the calculation ..::
def percentComplete = 0
Collection subTasks = issue.getSubTaskObjects()

for(subtask in subTasks) {
float weightVal = retrieveValue(subtask, weight)
float subCompletePercentVal = retrieveValue(subtask, subCompletePercent)
percentComplete += subCompletePercentVal * weightVal
}

// ::.. Return the result ..::
percentComplete

Just let me know if you have any questions about how to implement this. I've tested it in a local instance and it seems to work as expected / requested.

Hope this helps!

Steve

Hi Stephen, 

Thanks for this.

I actually wound up figuring out my own script, but yours and mine looks quite similar. 

Nice to know I did things correctly.

Here's what I wound up with:

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;

def percentCompleteField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12418");//Percent Complete
def weightField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_13500");//Weight

def percentComplete = 0.0;
boolean hasSubtasks = false;

Collection subTasks = issue.getSubTaskObjects();
for(subtask in subTasks) {
hasSubtasks = true;
def weight = (double)subtask.getCustomFieldValue(weightField);
def subPercentComplete = (double)subtask.getCustomFieldValue(percentCompleteField);
percentComplete += (weight / 100) * subPercentComplete;

}

percentComplete

if(hasSubtasks){
percentComplete;
} else {
(float)issue.getCustomFieldValue(percentCompleteField);
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,321 views 0 8
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you