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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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 }
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.  ¯\_(ツ)_/¯ 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Building a powerful Kanban dashboard in Jira with Great Gadgets app

Kanban is a popular agile framework suitable for both operations and development teams. It does not matter if you are a support team handling issues reported by customers, an internal service desk te...

56 views 0 0
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you