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

Automate Epic Custom Field Sum

Features
Listeners
Created 1 year ago, Updated 1 day(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira
Language |
groovy
def eventIssue = Issues.getByKey(issue.key as String)

//the name of the custom field. This code will work just as well with the custom field ID instead i.e. 10124L
def sumCustomFieldName = 'custom field name i.e. Sum of values'

//for regular issues the parent is an Epic
def epicKey = eventIssue.getParentObject()?.key ?: null

if(!epicKey) {
    // Checks the 'Epic Link' custom field
    epicKey = eventIssue.getEpic()?.key ?: null
}

if (!epicKey && eventIssue.issueType.subtask) {
    epicKey = eventIssue.parentObject?.epic?.key ?: null
}

if (!epicKey) {
    logger.info("We did not find an Epic in the hierarchy of this issue. Exiting.")
    return
}

//find all issue linked to the epic, taking care to exclude the epic itself as we sum over this list
def epicIssue
def sum = Issues.search("linkedissue = ${epicKey}").sum { issue ->
    if (issue.key == epicKey) {
        epicIssue = issue // Store the epic issue
        return 0 // Exclude the epic issue from the sum
    }
    return issue.getCustomFieldValue(sumCustomFieldName) ?: 0 // Safely sum non-epic values
}

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