It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Suresh Nov 09, 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

3 votes
Answer 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

Suresh Nov 19, 2017 • edited

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 Nov 19, 2017

Suresh,

Please use getIssueLinks(), to get all issues.

Refer here for more details.

Thanks

Praveen.

Suresh Nov 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 Nov 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 Nov 21, 2017 • edited

@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

Suresh Nov 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

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 Nov 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
}

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 Dec 13, 2017

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

NICE!!!

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

3 votes
Praveen Nov 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 Nov 12, 2017 • edited

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 Nov 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 Nov 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

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 Nov 13, 2017

thank you Deniel,

I have already used that method, but no luck.

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

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 Nov 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

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

0 votes
Mikhail Kurskiy Apr 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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

920 views 12 15
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you