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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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.
Apr 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

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.
Apr 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

Thanks Nic, how would you recommend rewriting the code?

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.
Apr 25, 2018 • edited

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

 

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.
Apr 25, 2018

you forgot to include an import:

import com.atlassian.jira.issue.Issue
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.
Apr 25, 2018

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

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.
Apr 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

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.
Apr 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
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 08, 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?

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 08, 2018

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

Atlassian Community Events