Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,364,939
Community Members
 
Community Events
168
Community Groups

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

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

0 votes

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
}

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

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 }

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events