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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,552,516
Community Members
 
Community Events
184
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 05, 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

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

cool (y) 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events