Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Scripted Field - How do I returned linked Issue count based on conditions of linked issues

Hi there,

Using Scriptrunner for Jira Server, I'm trying to return a number count of linked issues if those linked issues meet certain conditional requirements.

Namely, I'm looking to input a count into a scripted field (one with a Number Field template called "Upsell Count"). This script should only run on "Invoice" issuetypes.

The count should = the number of "Upsell" issues linked to the "Invoice" if the resolutions of the "Upsell" are NOT in ("Declined", "Deprecated").

I'm stuck on the resolution conditioning. Here's what I have so far. Anyone have ideas on how to accomplish what I'm looking for and/or improve what I already have?

import com.atlassian.jira.component.ComponentAccessor

if( == "Invoice")
int x = 0
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkedIssues = ComponentAccessor.getIssueLinkManager().getLinkCollection(issue, currentUser).getAllIssues()
if (linkedIssues.size() > 0){
for (int i = 0; i < linkedIssues.size(); i++){
if (linkedIssues[i].getIssueType().getName() == "Upsell"){
x = x + 1

if (x > 0){
return x
}else return null

Thanks so much! My org is on Jira Server v7.12.3.


1 answer

0 votes

Why not take advantage of groovy methods?

linkedIssues.count{ linkedIssue -> == "Upsell" && ! in (["Declined", "Deprecated"])

Hi Peter, 

Apologies, but I don't have much experience with groovy. Would your suggested block replace my entire script, or amend a certain part of it? Thanks.



Here is what your full script would look like

import com.atlassian.jira.component.ComponentAccessor

if( == "Invoice"){
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def linkedIssues = ComponentAccessor.issueLinkManager.getLinkCollection(issue, currentUser).allIssues
return linkedIssues.count{ linkedIssue -> == "Upsell" && ! in (["Declined", "Deprecated"])
} ? 0 : null
} else {
return null

The "? 0 : null" part is to return null when the count is zero.

You may also consider using the followin line instead of the other 'def linkedIssues =' above depending on whether you want to have the counts be impacted by the current user's permissions (if you use issue-level security). In this case, you can even get rid of the 'def currentUser' line.

def linkedIssues = ComponentAccessor.issueLinkManager.getLinkCollectionOverrideSecurity(issue).allIssues


Thanks so much for your help. I'll try this out and follow up. 



Darn, I'm not seeing errors to debug, but it also is only returning a 0 or null when I preview it. I took this whole line out to see if the problem was the extra condition or something else, but it was still just returning 0.

&& ! in (["Declined", "Deprecated"])

 Let me know if you have any other ideas.



I see a mistake I made on that line, it should not have parenthesis.


&& ! in ["Declined", "Deprecated"]


But generally, when I get into this sort of situation I do something like this

import com.atlassian.jira.component.ComponentAccessor
import com.apache.log4j.Level
log.setLevel(Level.DEBUG) //bump up the log level

log.debug " = $"
if( == "Invoice"){
log.debug "Invoice detected"
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def linkedIssues = ComponentAccessor.issueLinkManager.getLinkCollection(issue, currentUser).allIssues
log.debug "linked issues total count: ${linkedIssue.size()"
return linkedIssues.count{ linkedIssue ->
log.debug "Analysing $linkedIssue.key"
log.debug "\tissueType.Name = $"
log.debug "\tresolution = $"
log.debug "\tresolution in ["Declined", "Deprecated"] = $(! in ["Declined", "Deprecated"])}" == "Upsell" && ! in ["Declined", "Deprecated"]
} ? 0 : null
} else {
log.debug "Invoice NOT detected"
return null

Then I look through the logs and see where my logic when wrong. 

Like Azfar Masut likes this

Suggest an answer

Log in or Sign up to answer

Atlassian Community Events