Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scripted Fields: getting sum of custom field based on both link type and target issue type

Tomáš Vrabec March 5, 2021

Hi there,

after two days of googling and investigations and so many failed attempts I am asking the most wisdom community in the world.

The usacase is simple:

I have an issue type "Objective" with Scripted field "Objective points"

This "Objective points" should sum-up all "Feature points" from all epics linked thru issue link type 10308.

Here is my script inspired by Adaptavist library: 

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

final issueTypeName = "Objective" // The issue type for which we want the scripted field to be displayed
final linkedIssueType = "Epic" // The linked issues with that issue type will used
final customFieldName = "Feature Points" // The values of that custom field - of type number - we want to sum up
int issueLinkType = 10308

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 customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(linkedIssues.first().destinationObject).findByName(customFieldName)
if (!customField) {
log.debug "Custom field is not configured for that context"
return null
}

linkedIssues*.destinationObject.sum { Issue it -> it.getCustomFieldValue(customField) ?: 0 }

The problem is, that this will sum-up all Epics, not only those linked thru 10308.

I did various experiments with getIssueLinks(issueLinkType) or even with getting all outwardLinks as list and finding in them via for item in outwardLinks blahblah linkedIssues.add(result) but that failed on summing part which was unable to reach CFs - I do not figure out why.

If you know any elegant way, you help will be appreciated. Probably I am missing something very stupid and I was almost there :-/

Thanks, Tom

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 5, 2021

In your initial call to get links, why don't you just filter by both the link type and the linked issue type?

def linkedIssues = issueLinkManager.getOutwardLinks(issue.id).findAll { 
it.linkTypeId == issueLinkType && it.destinationObject.issueType.name == linkedIssueType
}
Tomáš Vrabec March 6, 2021

I tried, doesnt work. Instance is very outdated. Both SR and Jira. The remove works fine.  ¯\_(ツ)_/¯ 

0 votes
Tomáš Vrabec March 5, 2021

Just after posting that got an idea of removing link from the list and looks like it works.

What do you think?

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

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

final issueTypeName = "Objective" // The issue type for which we want the scripted field to be displayed
final linkedIssueType = "Epic" // The linked issues with that issue type will used
final customFieldName = "Feature Points" // The values of that custom field - of type number - we want to sum up
int issueLinkType = 10308

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

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

issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.id != issueLinkType) {
linkedIssues.remove(issueLink)}}

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

linkedIssues*.destinationObject.sum { Issue it -> it.getCustomFieldValue(customField) ?: 0 }
TAGS
AUG Leaders

Atlassian Community Events