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

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

Can scriptrunner create issue in epic only if a certain issue type is not already linked?

Hi there,

I am setting up a transition that will auto-create a set of issues and add them to an epic, using the built in "Clones an issue and links" post-function.

I have a set of custom issue types for this, like "Training" and "Data," and the create and link is working fine.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.link.IssueLinkManager
import org.apache.log4j.Level
import org.apache.log4j.Logger
Logger.getLogger("com.onresolve.scriptrunner").setLevel(Level.INFO)
 
Issue issue  = issue
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link')
issue.summary = 'Training for ' + sourceIssue.key
checkAttachment = {attachment -> false}
issue.assignee = null
checkLink = {link -> false}
issue.setCustomFieldValue(epicLink, sourceIssue)

What I need to do is check if there's already a "Training" or whatever issue linked to the epic, so that I don't create duplicates.

I tried to write a condition, but this does not work, and the log says it does not return a Boolean:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.link.IssueLinkManager
import org.apache.log4j.Level
import org.apache.log4j.Logger
Logger.getLogger("com.onresolve.scriptrunner").setLevel(Level.DEBUG)

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getInwardLinks(issue.getId())
boolean passesCondition = true
issueLinkManager.getInwardLinks(issue.id).each {issueLink ->    
    def linkedIssue = issueLink.destinationObject 
    String issueType = linkedIssue.getIssueType()
    log.info "Issuetype is " + issueType
    if (issueType == "Training")
    passesCondition = false
}

 

Even the logging isn't really working, so I must be saying something stupid.

Is there some simple way to check for a duplicate here?

Thanks!

 

3 answers

The method getIssueType() on an issue returns an IssueType object, not a string. So you should write

...
String issueType = linkedIssue.getIssueType().getName()
...

Hello April,

I get an error when I try your code, coming from 

issueLink ->

What does the -&gt do? My script is not recognizing as valid syntax

-&gt should be written as ->

Hi Marc,

Thanks for your response! Actually, that throws the exactly same error, but I tried saying this another way, and this seems to work:

import com.atlassian.jira.component.ComponentAccessor
// Check if instance of this issue type already exists
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getInwardLinks(issue.getId())
!(issueLinkManager.getInwardLinks(issue.id).each {issueLink ->
	def linkedIssue = issueLink.destinationObject
	String issueType = linkedIssue.getIssueType().getName()
	issueType == "Training"})

 So apparently, one cannot use a Boolean value here, but only test the truth of an entire statement.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍂📹 Apptoberfest demo contest roundup: vote for your favorite demos!

Hi Community! The submissions are in (and listed below) for the 🍻🍂Apptoberfest🍂🍻 Demo Competition and it’s time for you to place your votes for the best: Analytics & reporting app demo ...

216 views 5 14
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