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

This widget could not be displayed.

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)

This widget could not be displayed.

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();

This widget could not be displayed.
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?

This widget could not be displayed.

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?

This widget could not be displayed.

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 Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted 14 hours ago in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

41 views 1 0
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