Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

0 votes

Why not take advantage of groovy methods?

linkedIssues.count{ linkedIssue ->
linkedIssue.issueType.name == "Upsell" && !linkedIssue.resolution.name 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.

Best,

Andrew

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

Peter, 

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

Cheers,

Andrew

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

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. 

Suggest an answer

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

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

321 views 9 7
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