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!
Why not take advantage of groovy methods?
linkedIssues.count{ linkedIssue ->
linkedIssue.issueType.name == "Upsell" && !linkedIssue.resolution.name in (["Declined", "Deprecated"])
}
I'll try it out!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Peter,
Thanks so much for your help. I'll try this out and follow up.
Cheers,
Andrew
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.