Scripted Field to return number of cloned issues not working

My script to return the number of cloned issues not working as expected. it always returns 1.

Anyone know what the error might be? Also if I need to display the number of links that 'depend on' another link what do I replace the 'Cloners' with?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager

ComponentAccessor componentAccessor= ComponentAccessor.newInstance()

Collection<IssueLinkType> issueLinkTypes=componentAccessor.getComponentOfType(IssueLinkTypeManager.class).getIssueLinkTypes();
count = 0;
for (IssueLinkType linktype : issueLinkTypes) {
    String name=linktype.getName();
    if(name.equals("Cloners")){
        count++;
    }
}

return count;

5 answers

1 accepted

Hi, Thanks a lot. Finally got it working. :)

I was running it as a scripted field. I was messing up the Outward and Inward Links. I got it to work for 'depends on' like this:

import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getOutwardLinks(issue.id)

def subElements = issueLinks.findAll {
    it.getIssueLinkType().getName() == "Dependency"
}

return subElements.size()

I get the error

groovy.lang.MissingPropertyException: No such property: issue for class: Script5
at Script5.run(Script5.groovy:4)

to get the issue link id's bi-directionally as follows

to get OUward links
List<IssueLink> allOutIssueLink = issueLinkManager.getOutwardLinks(sourceIssue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
    IssueLink issueLink = (IssueLink) outIterator.next();
    System.out.println(issueLink.getIssueLinkType().getId());   
}

same way to get inword issueLink's

List<IssueLink> allInIssueLink = issueLinkManager.getInwardLinks(sourceIssue.getId());
for (Iterator<IssueLink> inIterator = allInIssueLink.iterator(); inIterator.hasNext();) {
    IssueLink issueLink = (IssueLink) inIterator.next();
    System.out.println(issueLink.getIssueLinkType().getId());
	if (issueLink.getIssueLinkType().getId() == 10) {
	  Issue destinationIssue = issueLink.getDestinationObject();
	  }
}

check this

https://developer.atlassian.com/static/javadoc/jira/5.0.5/reference/com/atlassian/jira/issue/link/IssueLinkManager.html#getIssueLinks%2528java.lang.Long%2529

Hi, Thanks for your ans.

I tried to run the code but Im getting this error:

javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: issueLinkManager for class: Script21

Do you know what might be the issue?

you have to get issuelinkmanager as follows

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();

import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category
import com.atlassian.jira.component.ComponentAccessor	

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def issueLinks = issueLinkManager.getOutwardLinks(issue.id)

def subElements = issueLinks.findAll {
        it.getIssueLinkType().getName() == "Cloners"
}

subElements.size()

if you need to look for different link types just change the filter in the findAll closure to some other names (you can find them in Administration > System > Issue Features > Issue Linking).

For 'depends on' it would be probably

def subElements = issueLinks.findAll {
        it.getIssueLinkType().getName() == "depends on"
}

Best regards

Do you run your code in a post function, custom JQL function, scripted field or what is the context? It may be that issue.id is null in your case.

You may look for InwardLinks, try this

def issueLinks = issueLinkManager.getInwardLinks(issue.id)

Hi, Thanks for the help.

I tried to run the code for "Cloners", but Im getting 0 as the output for both number of issueLinks and and subElements.size().

Do you know what might mbe the issue?

Hi,

Your script displays just the link types configured in the system. Thats why it is always 1.

But I suppose that you would like to display the number of clone issues connected to a given issue?

Hi,

Yes I need to get the number of cloned issues. (by counting the number of cloned issue links).

Do you how I could edit my code to achieve that ?

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,981 views 12 18
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot