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

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

Scriptrunner- Field to show percentage of Subtasks complete

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

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

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

Script_error_2.pngScript_Error_1.png

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

Thanks Nic, how would you recommend rewriting the code?

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

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

 

you forgot to include an import:

import com.atlassian.jira.issue.Issue

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

def completedSubTasks = issue.getSubTaskObjects().findAll{(Issue) it.getResolution() != null}.size()

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

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 likes this

10 out of 15 times failed

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

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 likes this

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?

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

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

{
    "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"
}

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

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

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
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

101 views 1 7
Read article

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