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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,555,636
Community Members
 
Community Events
184
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
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.
Mar 05, 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
}

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
AUG Leaders

Atlassian Community Events