How to retrieve a custom field values from several linked issues and present it as list in one scripted field?

Hi

I'm trying to activate a scripted field that will retrieve a custom field value from several linked issues (different link types - Parent of, Child of etc..) and present the scripted field values as a list of values (as a string/list).

Trying the following code which gives me only one of the wanted values (the value from the last linked issue):

 

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue

def Issue issue = issue

def user = ComponentAccessor.getUserManager().getUserByName("admin").getDirectoryUser()
def issueLinkManager = ComponentAccessor.issueLinkManager

def linkCollection = issueLinkManager.getLinkCollection(issue, user)

if (linkCollection && linkCollection.allIssues) {
def CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObject("customfield_xxxxx")
def linkedIssue = linkCollection.allIssues.first()
return linkedIssue.getCustomFieldValue(customField)
}

2 answers

1 accepted

1 vote
Vasiliy Zverev Community Champion Dec 15, 2015
  1. You miss a brasket into the very end of an issue.
  2. Try using methods to get value. E.g. linkCollection.getAllIssues(), but not allIssues

To speed up development see this:https://answers.atlassian.com/questions/32982259

I modify it a little to get value from first linked issue into list.

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue


def Issue issue = issue;
def user = ComponentAccessor.getUserManager().getUserByName("vzverev").getDirectoryUser()
def issueLinkManager = ComponentAccessor.issueLinkManager
def linkCollection = issueLinkManager.getLinkCollection(issue, user)
if (linkCollection && linkCollection.getAllIssues()) {    
        def CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
        def customField = customFieldManager.getCustomFieldObject("customfield_xxxx")        
    	for(Issue curLinkedissue: linkCollection.getAllIssues()){       	 
           	return curLinkedissue.getCustomFieldValue(customField)
    }
}

Thank you Vasily,

Your help was very useful and I eventually used the aforementioned code with a little addition and it works well:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue


def Issue issue = issue;
def user = ComponentAccessor.getUserManager().getUserByName("xxxxx").getDirectoryUser()
def issueLinkManager = ComponentAccessor.issueLinkManager
def linkCollection = issueLinkManager.getLinkCollection(issue, user)
if (linkCollection && linkCollection.getAllIssues()) {
def CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
def customField = customFieldManager.getCustomFieldObject("customfield_xxxxx")
def references = []
def curValue
for(Issue curLinkedissue: linkCollection.getAllIssues()){
curValue = curLinkedissue.getCustomFieldValue(customField)
if (curValue != null) {
references << curValue
}
}
String poReferences = references.join(",\r\n")
return poReferences
}

Vasiliy Zverev Community Champion Dec 16, 2015

Perfect!

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

1,001 views 3 12
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you