How to get list of Linked issue keys based linked issue custom field value?

Suresh November 9, 2017

Hi Team,
On current issue.
I have a field called 'Liked Issue Keys' and it should display only linked issue keys with comma separated values based on linked issues custom fieldvalue(check box-A)= Yes.

Where were linked issues (custom field )checkbox = Yes, then the 'Linked Issue Keys' should only show those issuekeys.

Another custom field(check box-B)  on current issue, should be checked automatically if 'Linked Issue Keys' !=null and if it is null and automatically unselect the checkbox-B.

Regards,

Suresh

4 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
3 votes
Answer accepted
Aidan Derossett [Adaptavist]
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.
November 14, 2017


Hey Suresh,

It looks like you're pretty close! You just may be overcomplicating it a bit. :D

I tried this out myself and came up with this:

import com.atlassian.jira.component.ComponentAccessor

//Get necessary managers
def cfm = ComponentAccessor.customFieldManager
def linkManager = ComponentAccessor.issueLinkManager

//Get check box field
def checkBoxA = cfm.getCustomFieldObjectByName("Check-Box-A") //Name of CF

def issueKeys = []
//Go through all linked issues and collect their keys if CheckBoxA is valued "Yes"
linkManager.getOutwardLinks(issue.id).each{
def linkedIssue = it.destinationObject
def checkBoxAValue = linkedIssue.getCustomFieldValue(checkBoxA)
//Checks that "Yes" is a selected value for check box A
if("Yes" in checkBoxAValue.collect{it.toString()})
{
issueKeys.add("${linkedIssue.key}")
}
}

if(issueKeys.size() > 0)
{
return issueKeys.join(',')
}
else
{
return null
}

I haven't completely tested this yet, but that should theoretically get you all of the keys from linked issues that have the value "Yes" for check box A.

Best,

Aidan

Suresh November 19, 2017

Thank you Aidan,

May I know the method to get In &Out wards linked issues.The Outwards method is getting linked issues list from current issue.Can any method will help us to get the issue linked in somewhere where Checkbox=yes

I am trying to get all linked issues.

linkManager.getInAndOutwardLinks(issue.id).each{

 

Praveen November 19, 2017

Suresh,

Please use getIssueLinks(), to get all issues.

Refer here for more details.

Thanks

Praveen.

Suresh November 19, 2017

Thank you praveen, We have already tried this method, it is returning null. We may need to pass issuelinktype.id args to this method.looking on how to retrieve linktypeid, from that issue.id and return keys

Suresh November 20, 2017

One more thing I would like to mention here, 
On edit action the script custom field not updating, is this limitation/something can be doable. 

Suresh November 21, 2017

@Aidan Derossett [Adaptavist] Can you help me to retrieve all linked issue where linkedissue linktype ="Related to RoadMap" and check-Box-A =Yes.

Aidan Derossett [Adaptavist]
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.
November 21, 2017

Hey again Suresh!

To get the inward links, you can use the same form as for getting outward links. So instead of "getOutwardLinks," you would use "getInwardLinks." And for inward links, you'd want the "linkedIssue" variable to be the sourceObject, not the destinationObject. Finally, to check for the link type, you simply need to do a comparison with the link type object's name:

//For example
if(it.issueLinkType == "Related to RoadMap")
{
//Do things
}

So all-in-all you'll need to change/add the following in your code:

linkManager.getOutwardLinks(issue.id).each{
def linkedIssue = it.destinationObject
def checkBoxAValue = linkedIssue.getCustomFieldValue(checkBoxA)

if(it.issueLinkType.name == "Related to RoadMap")
{
//Checks that "Yes" is a selected value for check box A
if("Yes" in checkBoxAValue.collect{it.toString()})
{
issueKeys.add("${linkedIssue.key}")
}
}
}
linkManager.getInwardLinks(issue.id).each{
def linkedIssue = it.sourceObject
def checkBoxAValue = linkedIssue.getCustomFieldValue(checkBoxA)

if(it.issueLinkType.name == "Related to RoadMap")
{
//Checks that "Yes" is a selected value for check box A
if("Yes" in checkBoxAValue.collect{it.toString()})
{
issueKeys.add("${linkedIssue.key}")
}
}
}

Try that out and see if that get's what you're looking for! :D

Best of Luck,

Aidan

Suresh November 21, 2017

It's working! thank you Aidan,

I have been calling "destinationObject" for InwardLinks method, and now realized that we need to call SoureObject.

Regards,

Suresh

Aidan Derossett [Adaptavist]
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.
November 22, 2017

Awesome! No problem Suresh! :D

The methods are a little tricky to work with at first, so I understand the confusion :)

Could you please accept this answer so that it will appear as answered for others who may be having similar issues?

Glad I could help!

Aidan

Suresh November 30, 2017

Hi @Aidan Derossett [Adaptavist] Can we sort the linked issue keys list based on issue numbers?

if(issueKeys.size() > 0)
{
return issueKeys.join(',')
}
else
{
return null
}
Aidan Derossett [Adaptavist]
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 6, 2017

Hey there,

You absolutely can :D

Using Groovy, you can do some sorting magic on the issueKeys array:

issueKeys.sort{a, b ->
a.split("-")[1] <=> b.split("-")[1]
}

I think that should do it. Give it a go and make sure nothing explodes haha.

Suresh December 13, 2017

@Aidan Derossett [Adaptavist] it worked!! thank you such :)

Aidan Derossett [Adaptavist]
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 19, 2017

NICE!!!

No problem, let me know if you have any other troubles :D

3 votes
Praveen November 10, 2017

Hi Suresh,

This can be done easily with an add on called Scriptrunner. Please go through the documentation for it and you will be able to understand on how to do it.

Suresh November 12, 2017

Thank you Praveen,

I have started as below, first I am trying return only linked issue to the script field.The following code returning only one issue, How to return all linked issues those related to linktype. 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.UpdateIssueRequest
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def linkedIssue;

issueLinkManager.getInwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Related To RoadMapItem") {
linkedIssue = issueLink.getSourceObject()

}
}
return linkedIssue

 

Praveen November 12, 2017

Hi again,

The below code will return the list of issues, if you need the objects of them you have to iterate the list.

def linkedIssues=issueLinkManager.getInwardLinks(issue.id)
Suresh November 13, 2017

I am trying as following.But it is returning no result.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def linkedIssues
def keys
linkedIssues=issueLinkManager.getInwardLinks(issue.id)
issueLinkManager.getInwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Related To RoadMapItem") {
for(Iterator outIterator = linkedIssues.iterator(); outIterator.hasNext();){
IssueLink issueLink2 = (IssueLink) outIterator.next();
linkedIssues = issueLink2.getSourceObject().getKey();
StringBuilder builder = new StringBuilder();
for(String s : linkedIssues) {
builder.append(s);
}
keys = builder.toString();
}
}
}
return keys
Daniel Yelamos [Adaptavist]
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.
November 13, 2017

Suresh:

In JIRA there can be outbound links and inward links.

If inwardlinks isn't getting you what you want, use the outwardlinks like so:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def linkedIssues
def keys
linkedIssues=issueLinkManager.getOutwardLinks(issue.id)
issueLinkManager.getInwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Related To RoadMapItem") {
for(Iterator outIterator = linkedIssues.iterator(); outIterator.hasNext();){
IssueLink issueLink2 = (IssueLink) outIterator.next();
linkedIssues = issueLink2.getSourceObject().getKey();
StringBuilder builder = new StringBuilder();
for(String s : linkedIssues) {
builder.append(s);
}
keys = builder.toString();
}
}
}
return keys

Hope this helps:

Dyelamos

Suresh November 13, 2017

thank you Deniel,

I have already used that method, but no luck.

Praveen November 13, 2017

Suresh,

 

Please try this

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
//def linkedIssues
def keys
//linkedIssues=issueLinkManager.getInwardLinks(issue.id)
def linkedIssues = issueLinkManager.getInwardLinks(issue.getId())

if (linkedIssues!=null){
for(int a=0; a<linkedIssues.size(); a++){
if (linkedIssues[a].issueLinkType.name == "Related To RoadMapItem"){
IssueLink issueLink2 = (IssueLink) outIterator.next();
linkedIssues = issueLink2.getSourceObject().getKey();
StringBuilder builder = new StringBuilder();
for(String s : linkedIssues) {
builder.append(s);
}
keys = builder.toString();
}

}
}
return keys

/*issueLinkManager.getInwardLinks(issue.id).each {issueLink ->
if (issueLink.issueLinkType.name == "Related To RoadMapItem") {
for(Iterator outIterator = linkedIssues.iterator(); outIterator.hasNext();){
IssueLink issueLink2 = (IssueLink) outIterator.next();
linkedIssues = issueLink2.getSourceObject().getKey();
StringBuilder builder = new StringBuilder();
for(String s : linkedIssues) {
builder.append(s);
}
keys = builder.toString();
}
}
}*/

Daniel Yelamos [Adaptavist]
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.
November 13, 2017

Suresh.

What if you remove the if clause:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def linkedIssues = issueLinkManager.getOutwardLinks(issue.id)
def keys
issueLinkManager.getInwardLinks(issue.id).each {issueLink ->
log.debug issue.issueLinkType.name
for(Iterator outIterator = linkedIssues.iterator(); outIterator.hasNext();){
IssueLink issueLink2 = (IssueLink) outIterator.next()
linkedIssues = issueLink2.getSourceObject().getKey()
StringBuilder builder = new StringBuilder()
for(String s : linkedIssues) {
builder.append(s)
}
keys = builder.toString()
}

}
return keys

Does this return anything at least?

A common scriptbuilding case, would be for you to use a few issues as an example, and using the console, debug the simplest case, and then move up in complexity until you have what you want. Even us "experts" don't get what we want the first time we try.

Cheers!

DYelamos

Suresh November 13, 2017

Thank you Pavan, DYelamos,

I am getting object like as below and trying to get the key from below objects. I do not find any method to getkey from linked issue object.am i missing any this here.

[com.atlassian.jira.issue.link.IssueLinkImpl@a64103b[id=140208,sourceId=149223,destinationId=149229,issueLinkType=10800], 
com.atlassian.jira.issue.link.IssueLinkImpl@50487581[id=140210,sourceId=147897,destinationId=149229,issueLinkType=10800]]
0 votes
Mikhail Kurskiy April 19, 2018

Greetings all,
I have a quite same question, but not on custom fields. On ranking.
I'll be grateful for your help.
https://community.atlassian.com/t5/Questions/How-to-get-list-of-Linked-issues-which-are-quot-is-blocked-by/qaq-p/777864

0 votes
Daniel Yelamos [Adaptavist]
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.
November 14, 2017

Surreh:

That happens because links do not have keys, they have the JIRA numeric ID for issues. The link object does not contain two issues, but a reference to both of them in form of their numeric id.

What you need to do after you get the link is to do a:

IssueManager.getIssue(destinationId)

IssueManager.getIssue(sourceId)

That will fetch the right info for you.

Cheers!

Dyelamos

TAGS
AUG Leaders

Atlassian Community Events