You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.
View groupJoin the community to find out what other Atlassian users are discussing, debating and creating.
I've created a custom-field titled, "Capacity"
I'm trying to add up all sub-tasks with a value in "Capacity" on a user story into a custom field titled on issue-type story entitled - (Total Capacity).
The below code is what I've tried and I haven't had much luck.
Any assistance would be appreciated!
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
int totalSum = 0
for(Issue subtask: issue.getSubTaskObjects()){
CustomField Capacity = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Capacity")
if(subtask.getCustomFieldValue(Capacity) != null) {
totalSum += subtask.getCustomFieldValue(Capacity).toString().toInteger()
}
}
//return totalSum
I like doing things the groovy way ... I think this should work:
import com.atlassian.jira.component.ComponentAccessor
def capacity = ComponentAccessor.customfieldManager.getCustomFieldObjectByName('Capacity')
def totalSum = issue.subTaskObjects.sum{
it.getCustomFieldValue(capacity)?.toInteger() ?: 0
} ?: 0
totalSum
The magic is in the "sum" method and the elvis operator ?:
The sum closure iterates over the subtasks and returns the capacity value or 0 if null.
The last elvis will return 0 in cases where there are no subtasks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Attempted to change out the values as you suggested, and received the above error.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oops, my bad.
Made a small error when transferring and adapting my example code from my environment.
Response above updated with
def capacity = ComponentAccessor.customfieldManager.getCustomFieldObjectByName('Capacity')
Bu what Tanya said is also true. This won't work if you try to report on this value since scripted field values are not actually stored in the database. They get calculated when you view the issue and when the issue is indexed (calculated value is available for searching, but won't get updated in the index when subtasks are modified).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The thing is that if you only need to display the capacity field then it's ok to use scripted field for that. But if you actually want to store this valie and to be able to use it in reports or dashboard you should use a regular number type field and set it's value from a scripted listener or escalation service.
Tanya
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.