Scriptrunner- Field to show percentage of Subtasks complete

Sarah Pinns April 23, 2018

Is there a script field that could be configured to divide completed subtasks by total subtasks in one parent ticket?  This would be used as a column in filter and dashboard results.

1 answer

0 votes
Ivan Tovbin
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 23, 2018

Hi Sarah,

Indeed, a scripted field seems to be the solution here. As for the code, assuming a completed subtask is one with a resolution, try this:

int totalSubTasks = issue.getSubTaskObjects().size()
int completedSubTasks = totalSubTasks.findAll{it.getResolution != null}.size()
int result = (completedSubTasks*100) / totalSubTasks
return result
Sarah Pinns April 23, 2018

Thanks Ivan! I attached a couple photos of two errors I received with that script.  Any idea?

Script_error_2.pngScript_Error_1.png

Nic Brough -Adaptavist-
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 23, 2018

If you stick to Java, you need to either convert or cast between variable types (e.g. use java.math.BigDecimal.intValue() to convert the value to an int) or use the right types (replace the "int" at the front of line 2 with BigDecimal)

If you flip to Groovy, you can use untyped variables - replace the three "int"s with "def".  But you may need to cast the return value to the right type.  i.e.

return (long) result

Sarah Pinns April 25, 2018

Thanks Nic, how would you recommend rewriting the code?

Sarah Pinns April 25, 2018

There is still one error occurring2018-04-25_0914.png

Ivan Tovbin
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 25, 2018

Try this:

import com.atlassian.jira.issue.Issue

def totalSubTasks = issue.getSubTaskObjects().size()
def completedSubTasks = issue.getSubTaskObjects().findAll{(Issue) it.getResolution != null}.size()
def result = (completedSubTasks*100) / totalSubTasks
return (long) result

 

Sarah Pinns April 25, 2018

2018-04-25_0919.png

Ivan Tovbin
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 25, 2018

you forgot to include an import:

import com.atlassian.jira.issue.Issue
Sarah Pinns April 25, 2018

2018-04-25_0922.png

Ivan Tovbin
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 25, 2018

My mistake, forgot the parenthesis. It should be like this:

def completedSubTasks = issue.getSubTaskObjects().findAll{(Issue) it.getResolution() != null}.size()
Sarah Pinns April 25, 2018

Thanks! This got rid of the any errors! When I preview a ticket that has several subtasks, of which some are closed, it returns null.  Could this have to do with the Template selection?  Attached is what the logs returned2018-04-25_0931.png

Ivan Tovbin
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 25, 2018

Indeed, you need to switch your field template to 'Number Field' and then create the field before you can use the preview function. Also make sure you set the searcher of this field (Administration -> Issues -> Custom fields -> Edit field) to 'Number Searcher'

Like Benjamin Horst likes this
Sarah Pinns April 25, 2018

10 out of 15 times failed

2018-04-25_1616_001.png2018-04-25_1616.png

Ivan Tovbin
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 26, 2018

That error indicates that the script tried to calculate the percentage of completed subtasks in an issue where there are no subtasks at all. Let's add a check for that to the code:

import com.atlassian.jira.issue.Issue

def totalSubTasks = issue.getSubTaskObjects().size()
if (totalSubTasks > 0){
def completedSubTasks = issue.getSubTaskObjects().findAll{(Issue) it.getResolution() != null}.size()
def result = (completedSubTasks*100) / totalSubTasks
return (long) result
}
return null
Like Benjamin Horst likes this
Sarah Pinns April 26, 2018

2018-04-26_0848_001.png2018-04-26_0848.png

Sarah Pinns May 4, 2018

@Nic Brough -Adaptavist- @Ivan Tovbin any further ideas?

Ivan Tovbin
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.
May 8, 2018

Sorry for the delay,

Can you tell me if the field actually works? I mean if you ignore those errors, does it show you completed subtask percentage?

Sarah Pinns May 8, 2018

If all subtasks are open it shows 0 and if one subtask is closed and one is open, the field is null.

Ivan Tovbin
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.
May 8, 2018

Can you post the whole error message, I can't see it on your screenshots.

Sarah Pinns June 15, 2018

Cut_off_Payload.pngpayload.pnglogs_error.png

Sarah Pinns June 15, 2018
{
    "customField": "Percentage of Subtasks Complete (com.atlassian.jira.issue.fields.ImmutableCustomField)",
    "issue": "PVP-52 (com.atlassian.jira.issue.IssueImpl)",
    "log": "org.apache.log4j.Logger@21c84325",
    "componentManager": "com.atlassian.jira.ComponentManager@13d2cfcc",
    "getCustomFieldValue": "groovy.lang.Closure",
    "enableCache": "groovy.lang.Closure"
}
Christopher Gronde June 7, 2019

Did this ever get resolved?!  I am trying to do the same thing and I am getting the same error where if there is one open and one done it marks it as null but if all the subtasks are open then it shows 0

Dominique Eav September 6, 2019

Works for me with minor edits :

import com.atlassian.jira.issue.Issue

def totalSubTasks = issue.getSubTaskObjects().size()
if (totalSubTasks > 0){
def completedSubTasks = 0
for (subTask in issue.getSubTaskObjects()){
if (subTask.getResolution() != null){
completedSubTasks++
}
}
def result = (completedSubTasks*100) / totalSubTasks
return (long) result
}
return null
Like Edimara Souza likes this
Edimara Souza March 19, 2021

How this script works if I need to exclude resoltution empty and resolution = 'Removed'?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events