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
Accepted answer
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)
    }
}

Hi Vasiliy,

I am pretty stuck with a similar issue, is there a way we get custom field values from multiple linked issues(not just first linked issue custom field value). 

Thanks,
Vijay

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
Community showcase
Asked Thursday in Jira Ops

I'm John Allspaw, Ask Me Anything about incident analysis and postmortems

I'm John Allspaw, co-founder of   Adaptive Capacity Labs, where we help teams use their incidents to learn and improve. We bring research-driven methods and approaches to drive effective inciden...

4,339 views 20 13
View question

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