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

Eitan Gur December 15, 2015

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
Answer accepted
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 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)
    }
}
Vijay Sv October 12, 2018

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

0 votes
Eitan Gur December 16, 2015

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 16, 2015

Perfect!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events