Skip to main content
Example scripts
arrow icon
To homepage
Jira
Cloud icon
Cloud

Calculate the Sum of Fields from Multiple Issues from a JQL Query

Created 7 month(s) ago, Updated 8 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira
Language |
groovy
// sum up the values of this custom field
final customFieldName = 'Amount Paid'
final parentIssueKey = 'Parent Issue Key'

def parentIssue = Issues.getByKey(parentIssueKey)
def subtasks = parentIssue.subtasks

// if the issue doesn't have any sub-tasks or is a subtask itself then no need for action
if (subtasks.empty) {
    return
}
def customFieldId = parentIssue.getNames().find { it.value == customFieldName }.key
if (!customFieldId) {
    logger.info "Custom field with name $customFieldName is not configured for issue type ${parentIssue.issueType.name} and project ${parentIssue.getProjectObject().name}"
    return
}

def sum = subtasks.sum { subtask ->
    subtask.getCustomFieldValue(customFieldName) ?: 0
}

parentIssue.update {
    setCustomFieldValue(customFieldName, sum)
}
Having an issue with this script?
Report it here