How to check linked issue summary before creating issue?

Hi there,

I'm using the ScriptRunner post-function "Clones an issue and links." I need to create a set of linked issues, but the team wishes to avoid duplicates.

New issues will be created with a summary like:

issue.summary = 'Training for ' + sourceIssue.summary

I wrote the condition below to check linked issue summaries for a match:

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getOutwardLinks(issue.getId())
Issue issue  = issue
String sourceName = issue.getSummary()
log.info "Source name is " + sourceName
// Check if instance of this issue name already exists
!(issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->  
    def linkedIssue = issueLink.destinationObject
    String issueName = linkedIssue.getSummary()
    log.info "New issue name is " + issueName
    issueName == "Training for " + sourceName})

 

I see the values I expect in the log, but a duplicate issue is created anyway.

I also tried creating a second variable in case I was not comparing apples to apples somehow:

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getOutwardLinks(issue.getId())
Issue issue  = issue
String sourceName = issue.getSummary()
log.info "Source name is " + sourceName
String newName = "Training for " + sourceName
log.info "New name is " + newName
// newName returns the string I expect
// Check if instance of this issue name already exists
!(issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->  
    def linkedIssue = issueLink.destinationObject
    String issueName = linkedIssue.getSummary()
    log.info "New issue name is " + issueName
    issueName == newName})

 

I can see from the log that I'm definitely finding linked issues with the summary I don't want to duplicate:

 /secure/WorkflowUIDispatcher.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] New issue name is Training for test

But it seems finding does not mean actually preventing duplicate issues.

Anyone know what I'm doing wrong?

Thanks!

 

 

1 answer

1 accepted

Try this code to check dublicates:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink

String sourceName = issue.getSummary()
log.info "Source name is " + sourceName
String newName = "Training for " + sourceName
log.info "New name is " + newName

// newName returns the string I expect
// Check if instance of this issue name already exists
boolean isAlreadyExist = false;
for(IssueLink issueLink: ComponentAccessor.getIssueLinkManager().getOutwardLinks (issue.id)){
    if(newName == issueLink.destinationObject.getSummary()){
        isAlreadyExist = true;
        break;
    }
}

if(isAlreadyExist)
    log.info "isAlreadyExist"

Hey thanks!

Unfortunately, that returns "Condition did not return a boolean, coercing to false."

I've tried declaring a Boolean before as a condition in a workflow, which always returns that same error. I'm no programmer, but still, I'm a little confused about why the result of a Boolean is not a Boolean... (laughing).

I've only ever gotten this to work by doing something like "!(statement to evaluate)".

Trying to figure out how to restate what you have there....

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
 
String sourceName = issue.getSummary()
log.info "Source name is " + sourceName
String newName = "Training for " + sourceName
log.info "New name is " + newName
 
// newName returns the string I expect
// Check if instance of this issue name already exists
for(IssueLink issueLink: ComponentAccessor.getIssueLinkManager().getOutwardLinks (issue.id)){
    if(newName == issueLink.destinationObject.getSummary()){
        return true;    
    }
}
 
return false

 

  

Aha, that was it!

Except, whatever code is behind this condition seems to create an issue if the result is true, and not if the answer is false, so I just switched the logic, and it is working like this:

for(IssueLink issueLink: ComponentAccessor.getIssueLinkManager().getOutwardLinks (issue.id)){
    if(newName == issueLink.destinationObject.getSummary()){
        return false;    
    }
}
return true

Thanks so much for all your help!

THAT wasn't counter-intuitive at all smile

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

97 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you