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
}
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.