Forums

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

Sum customfield value of subtasks on parent issue

Joe Charman
Contributor
June 5, 2019

Hi,

 

I need to sum a customfield value on a list of subtasks which meet a criteria.

So for example, sum customfield_XXXX value from all subtasks of issue where sub task issue equals X.

 

I have this but it does not specify a sum of customfield in the subtask or filter by type:

 

if (issue.getIssueType().name != "Sub-task") { // only calculate this for non-subtask issues
def subTaskSum = 0
issue.getSubTaskObjects()?.each { subtask -> // go through all subTask issues
subTaskSum += 1
}
return subTaskSum
}

 

 

2 answers

0 votes
Radhika Vijji _Innovalog_
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.
June 5, 2019

Hi Joe,

You say that you are trying to check the criteria on the subtasks, but your code checks them on the current issue and hence your code returns 0 every time. I suggest you use the Groovy editor help of the app that can help you in writing your scripts. Try this code:

def subTaskSum = 0
issue.subTaskObjects.findAll{
it.issuetype.name == "Resource" & it.status.name == "Requested"
}.each{
subTaskSum += it.get("customfield_16503")
}
return subTaskSum

Regards,

Radhika 

Joe Charman
Contributor
June 6, 2019

Hi @Radhika Vijji _Innovalog_ 

 

That works great, thank you! How would I adjust this to list more than one status? 

This seems to return 0 again: 

 

def subTaskSum = 0
issue.subTaskObjects.findAll{
it.issuetype.name == "Resource" && it.status.name == "Requested" && it.status.name == "Testing"
}.each{
subTaskSum += it.get("customfield_16503").toInteger()
}
return subTaskSum
Radhika Vijji _Innovalog_
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.
June 6, 2019

Hi Joe,

An issue can be in one status only. Since your code is checking for the sub-task to be in two statuses at a time, which is impossible :), you are getting a resultant 0. You should use the "||" or "in" operator instead:

def subTaskSum = 0
issue.subTaskObjects.findAll{
it.issuetype.name == "Resource" && (it.status.name == "Requested" || it.status.name == "Testing")
}.each{
subTaskSum += it.get("customfield_16503").toInteger()
}
return subTaskSum

or

def subTaskSum = 0
issue.subTaskObjects.findAll{
it.issuetype.name == "Resource" && it.status.name in ["Requested","Testing"]
}.each{
subTaskSum += it.get("customfield_16503").toInteger()
}
return subTaskSum 

Regards,

Radhika 

0 votes
Nic Brough -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.
June 5, 2019

This script just counts each subtask and you could do it in one line - issue.getSubTaskObjects()?.size()

The .each loop needs to be more like this:

subtask -> 

if ( <something>)
subTaskSum += subtask.getCustomFieldValue(cf_xxxxx)?

Note that I've said <something> because "where sub task issue equals X" does not clearly define what the question is.

Check out https://library.adaptavist.com/entity/calculate-the-sum-of-all-values-of-a-custom-field-in-linked-issues - the loop they have is for linked issues instead of subtasks, but you only really need to borrow the sub-task sum code.

Joe Charman
Contributor
June 5, 2019

Thanks for your reply Nic,

Based on that, I've tried this but still with no luck:

if (issue.getIssueType().name != "Sub-task") { // only calculate this for non-subtask issues
def subTaskSum = 0
issue.getSubTaskObjects()?.each { subtask -> // go through all subTask issues

if(issue.issueType == "Resource" & issue.status == "Requested")
{
subTaskSum += issue.get("customfield_16503")
}
}
return subTaskSum
}

 

I'm using Jira Misc Custom Fields, if that makes a difference? Since I need this in a customfield for the issue search page.

 

The above returned a 0 for every issue

Nic Brough -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.
June 5, 2019

I don't know how JMCF might be handling some of those lines, so I am a bit stuck.

Your logic (or pseudocode) is now absolutely right.  Totally clear to us humans, and should work perfectly if the coding tool understood it right.

If it were for ScriptRunner, there's some stuff we would have to change, but it is purely "not knowing exactly how to do it", rather than doing something that the API doesn't do. 

But I do not know enough about JMCF to tell you which bits might be failing.  For example, issue.get("cf_#") is clear, but won't work in the core of Jira or ScriptRunner.  The .get is not a Jira API function, so SR won't handle it.  Where I fall over is that I do not know if it is a valid JMCF function.

Like Joe Charman likes this
David Fischer
Community Champion
June 6, 2019

Hi @Joe Charman ,

your code looks ok except for the fact that you're testing the issuetype of the main issue instead of the sub-task inside the "each" loop. Also, you forgot to add ".name" when accessing the issue type and status. Your code should rather be:

if (issue.getIssueType().name != "Sub-task") { // only calculate this for non-subtask issues
def subTaskSum = 0
issue.getSubTaskObjects()?.each { subtask -> // go through all subTask issues

if(subtask.issueType.name == "Resource" & subtask.status.name == "Requested")
{
subTaskSum += issue.get("customfield_16503")
}
}
return subTaskSum
}

 I would also make another subtle change, using

!issue.isSubTask()

to check whether the current issue is not a sub-task (1st line of your script).

Finally, all this will only work with JMCF 2.0 or above.

David Fischer
Community Champion
June 6, 2019

Oh, and "and" should be "&&", not "&":

if (!issue.isSubTask()) { // only calculate this for non-subtask issues
def subTaskSum = 0
issue.getSubTaskObjects()?.each { subtask -> // go through all subTask issues

if(subtask.issueType.name == "Resource" && subtask.status.name == "Requested")
{
subTaskSum += issue.get("customfield_16503")
}
}
return subTaskSum
}
Joe Charman
Contributor
June 6, 2019

Thank you very much @David Fischer 

Suggest an answer

Log in or Sign up to answer