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

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

Szymon F November 5, 2019

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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 5, 2019

Hi @Szymon F ,

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

Szymon F November 5, 2019

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 5, 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

Szymon F November 5, 2019

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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 5, 2019

cool (y) 

TAGS
AUG Leaders

Atlassian Community Events