Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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

1 vote
Answer 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 Sign up to answer
TAGS

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