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