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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

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

Scriptrunner help - limit script field results to specific project(s)

Hi Community

I need a little bit of help with a scripted field and looking for ways to limit the returned results to specific projects. My scriptrunner knowledge is very basic and have not been able to work this out.

Below is the code the script field - it simply returns the issue keys for specific link type (we have several script fields like this) and they're used for reporting. A team wants to limit the return values of specific projects only - so if one of their issues is linked to multiple projects that are linked via that type ; they only want to show the results for a specific project.

E.g. Issue ABC-123 is linked via 'delivers' type to DEF-456 ; XYZ-789 ; XYZ-890 but team only wants to know what XYZ issues are linked. The current script returns all delivers issues for ABC-123.

import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def retVal = ''

// Get only outward links
def links = issueLinkManager.getOutwardLinks(issue.id)

def itr = links.iterator()
while (itr.hasNext()) {
def link = itr.next()

// Only care about "Delivered by" link types
if (link.issueLinkType.name != 'Delivers') {
continue
}

// Get other issue
def linkedIssue = link.destinationObject

// Line seperators if more than one ticket
if (retVal != '') {
retVal += '<br>'
}
retVal += "${linkedIssue.getKey()} "
}

return retVal

This script field was setup by a previous admin so unable to get assistance from them. I looked at scriptrunner documentation and seen you can add JQL queries but I don't understand the code enough to put that into my existing script fields.

I appreciate any assistance the community can offer.

Regards, Jamie 

2 answers

1 accepted

0 votes
Answer accepted

Hi, try the following:

Note tht you must replace 'XYZ' with the desired project key to display

 

import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def retVal = ''

// Get only outward links
def links = issueLinkManager.getOutwardLinks(issue.id)

def itr = links.iterator()
while (itr.hasNext()) {
def link = itr.next()

// Only care about "Delivered by" link types
if (link.issueLinkType.name != 'Delivers') {
continue
}

// Only care about linked issue of 'XYZ' project
if (link.destinationObject.projectObject.key != 'XYZ') {
continue
}


// Get other issue
def linkedIssue = link.destinationObject

// Line seperators if more than one ticket
if (retVal != '') {
retVal += '<br>'
}
retVal += "${linkedIssue.getKey()} "
}

return retVal

 

Thanks @Jack Nolddor _Sweet Bananas_ for the fast reply. 

This works perfectly and meets the requirements for the teams that wanted this.

HI @Jack Nolddor _Sweet Bananas_ 

I have a followup question - if i wanted to include two or three project's issues in the returned results but exclude the rest is there any easy way to do this? 

E.g. I want to see linked issues from ABC, XYZ, JKL projects but not DEF or anyone else.

Would you be simply modifying this line in the script or adding additional code? 

// Only care about linked issue of 'XYZ' project
if (link.destinationObject.projectObject.key != 'XYZ') {
continue
}

I really appreciate you help with this.

Regards, Jamie 

Just use this version instead:

 

 

import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def cfManager = ComponentAccessor.getCustomFieldManager()

def retVal = ''

// Get only outward links
def links = issueLinkManager.getOutwardLinks(issue.id)

def itr = links.iterator()
while (itr.hasNext()) {
def link = itr.next()

// Only care about "Delivered by" link types
if (link.issueLinkType.name != 'Delivers') {
continue
}

// Only care about linked issue of given projects
def allowedProjects = ['ABC', 'XYZ', 'JKL']
if (!allowedProjects.contains(link.destinationObject.projectObject.key)) {
continue
}


// Get other issue
def linkedIssue = link.destinationObject

// Line seperators if more than one ticket
if (retVal != '') {
retVal += '<br>'
}
retVal += "${linkedIssue.getKey()} "
}

return retVal: :
Like Jamie Rogers likes this

Thanks again @Jack Nolddor _Sweet Bananas_ this does exactly what I need it to do.

You're welcome

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

How to create Jira issus from Excel file?

When to use CSV importer When managing your processes in Jira, there are many occasions where you need to create a lot of tasks. Creating them one by one will cost you a lot of time and effort and i...

4,623 views 22 33
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