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

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


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

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{

 

Suresh,

Please use getIssueLinks(), to get all issues.

Refer here for more details.

Thanks

Praveen.

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

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. 

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

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

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

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

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
}

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.

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

NICE!!!

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

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.

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

 

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)

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

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

thank you Deniel,

I have already used that method, but no luck.

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();
}
}
}*/

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

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

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Monday in Jira Ops

Jira Ops Early Access Program Update #1: Announcing our next feature and a new integration

Thanks for signing up for Jira Ops! I’m Matt Ryall, leader for the Jira Ops product team at Atlassian. Since this is a brand new product, we’ll be delivering improvements quickly and sharing updates...

392 views 0 8
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