Forums

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

Get custom field values for all subtasks using Groovy

Jake Jollimore October 17, 2017

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

1 vote
Stephen Cheesley [Adaptavist]
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.
October 23, 2017

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

Jake Jollimore October 23, 2017

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
TAGS
AUG Leaders

Atlassian Community Events