Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate the Sum of a Custom Field in Linked Issues in Scripted Field

Shahin Dogan November 29, 2022

Hello Atlassian Community,

I would like to calculate the sum of Story Points in all linked Issues with a scripted field via ScriptRunner. I´ve found this script: 

https://library.adaptavist.com/entity/calculate-the-sum-of-all-values-of-a-custom-field-in-linked-issues

and adjusted it to my use case like this: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

// The issue type for which we want the scripted field to be displayed
final issueTypeName = 'Story'

// The linked issues with that issue type will used

final linkedIssueType ???


// The values of that custom field - of type number - we want to sum up
final numberOfUsersFieldName = 'Story Points'

if (issue.issueType.name != issueTypeName) {
    return null
}

def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id).findAll { it.destinationObject.issueType.name == linkedIssueType }
if (!linkedIssues) {
    return null
}

def numberOfUsersField = ComponentAccessor.customFieldManager.getCustomFieldObjects(linkedIssues.first().destinationObject).findByName(numberOfUsersFieldName)
if (!numberOfUsersField) {
    log.debug "Custom field is not configured for that context"
    return null
}

linkedIssues*.destinationObject.sum { Issue it -> it.getCustomFieldValue(numberOfUsersField) ?: 0 }
The only thing I couldn´t adjust without an error message is, that I don´t got an explicit "linkedIssueType". As I mentioned in the beginning, it should calculate all linked issues, not an specific type. 
Does someone got an idea how to modify this script? I would be pleased about any tips.
Best regards,
Shahin

1 answer

1 accepted

1 vote
Answer accepted
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 29, 2022

Welcome to the Atlassian Community!

Just drop the code that looks for a specific type, and work with the full list of all links the "get" returns.

Shahin Dogan November 30, 2022

Hi @Nic Brough -Adaptavist-

I admit that I´m not a dev, but it worked after some trials. Thanks for your help!

Suggest an answer

Log in or Sign up to answer