Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,359,947
Community Members
 
Community Events
168
Community Groups

Scriptrunner Listner cloning issue when custom field has value and avoid duplicates

Edited

Hi Folks,

I am working on some simple script but it crashed some day ago and does not work anymore. 

This one should create a clone when a custom field is YES:

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


def customFieldManager = ComponentAccessor.getCustomFieldManager()
String customFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject((13207 as long)))


if (customFieldValue == "Yes")
{
return true
}

 

also I wanted to add if condition to avoid duplicates:

if (customFieldValue == "Yes"
&& !issueLinkManager.getOutwardLinks(issue.getId())*.destinationObject.projectObject.name.contains('Known Issues'))

{
return true
}

 

For now, the first part is working. Can you give me a hand how to add the second part?

1 answer

1 accepted

0 votes
Answer accepted
Leo Community Leader Nov 05, 2019

Hi @Szymon Fijalkowski ,

You can try out below snippet, I just included my logic in your code

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

def customFieldManager = ComponentAccessor.getCustomFieldManager()
String customFieldValue = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject((13207 as long)))

if (customFieldValue == "Yes" && !issueLinkManager.getInwardLinks(issue.getId())*.issueLinkType.name.contains('Cloners'))

{
return true
}

 

Note: I'm validating Inward link of event occurred ticket and passing Issue Link name which I used "Cloners" it has cloned by and clones link names as inward and outward 

Hope it gives you  some idea

 

BR,

Leo

I thought about something similar unfortunately the name can not be changed, even with some suffix or any tag.

Instead of name of issue/project may I use i.e. issue type? I have name = KI and id= 10902?

Leo Community Leader Nov 05, 2019

Then you may need to go with some huge script to iterate over linked issue and find their project name or issue type. I just written a script didn't validate it may need some minor changes. here I used both inward and outward functions. you can removed unwanted one based on your clone configuration but I would suggest you to keep both

import com.atlassian.jira.ComponentManager
import groovy.xml.MarkupBuilder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.link.LinkCollectionImpl;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.ListLinks")


def valid = true;
List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
;
def linkedIssue = issueLink.getDestinationObject()
String type = linkedIssue.getIssueType().getName();
String project = linkedIssue.getProject().name
if(project == "Target Project name"){
valid = false
}
}
List<IssueLink> allInIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allInIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
;
def linkedIssue = issueLink.getSourceObject()
String type = linkedIssue.getIssueType().getName();
String project = linkedIssue.getProject().name
if(project == "Target Project name"){
valid = false
}
}
if(valid){
return true
}

 

BR,

Leo

Dear @Leo ,

thank you so much! it looks impressive for me. As far as I have tried it works. Now I need to add condition for the custom field.

 

Thank you!

 

----------------Added----------------

import com.atlassian.jira.ComponentManager
import groovy.xml.MarkupBuilder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.link.LinkCollectionImpl;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.component.ComponentAccessor;

import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.ListLinks")

def customFieldManager = ComponentAccessor.getCustomFieldManager()
String cf = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject((13207 as long)))

if (
cf == "Yes")
{


def valid = true;
List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
;
def linkedIssue = issueLink.getDestinationObject()
String type = linkedIssue.getIssueType().getName();
String project = linkedIssue.getProjectObject().name
if(project == "Known Issues"){
valid = false
}
}
List<IssueLink> allInIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
for (Iterator<IssueLink> outIterator = allInIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
;
def linkedIssue = issueLink.getSourceObject()
String type = linkedIssue.getIssueType().getName();
String project = linkedIssue.getProjectObject().name
if(project == "Known Issues"){
valid = false
}
}
if(valid){
return true
}
}
else false

 

Leo Community Leader Nov 05, 2019

cool (y) 

Suggest an answer

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

Online AMA this week: Your project management questions answered by Jira Design Lead James Rotanson

We know that great teams require amazing project management chops. It's no surprise that great teams who use Jira have strong project managers, effective workflows, and secrets that bring planning ...

179 views 1 6
Read article

Atlassian Community Events