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

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

Andrew _Zim_ Zimmerman _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 29, 2019

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(issue.issueType.name == "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.

Cheers!

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 29, 2019

Why not take advantage of groovy methods?

linkedIssues.count{ linkedIssue ->
linkedIssue.issueType.name == "Upsell" && !linkedIssue.resolution.name in (["Declined", "Deprecated"])
}
Andrew _Zim_ Zimmerman _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 29, 2019

I'll try it out!

Andrew _Zim_ Zimmerman _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 29, 2019

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.

Best,

Andrew

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 29, 2019

Here is what your full script would look like

import com.atlassian.jira.component.ComponentAccessor

if(issue.issueType.name == "Invoice"){
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def linkedIssues = ComponentAccessor.issueLinkManager.getLinkCollection(issue, currentUser).allIssues
return linkedIssues.count{ linkedIssue ->
linkedIssue.issueType.name == "Upsell" && !linkedIssue.resolution.name 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
Andrew _Zim_ Zimmerman _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 29, 2019

Peter, 

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

Cheers,

Andrew

Andrew _Zim_ Zimmerman _Appfire_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 29, 2019

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.

&& !linkedIssue.resolution.name in (["Declined", "Deprecated"])

 Let me know if you have any other ideas.

Cheers,

Andrew

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 1, 2019

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

Try

&& !linkedIssue.resolution.name 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 "issueType.name = $issue.issueType.name"
if(issue.issueType.name == "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 = $linkedIssue.issueType.name"
log.debug "\tresolution = $linkedIssue.resolution.name"
log.debug "\tresolution in ["Declined", "Deprecated"] = $(!linkedIssue.resolution.name in ["Declined", "Deprecated"])}"
linkedIssue.issueType.name == "Upsell" && !linkedIssue.resolution.name 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
TAGS
AUG Leaders

Atlassian Community Events