Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy for boolean clone check

Cailin Che
Contributor
April 28, 2023

Hello!

I'm trying to write a postfunction for my workflow that will clear fields from cloned issues when they're created. We have JMWE, JSU, and Scriptrunner to work with, but I am not very savvy with Groovy and am having trouble scripting a condition that will tell me if the created issue is linked to anything else as "clones". Our users change the summary, so checking the summary for "CLONE -" is not an option.

Thanks in advance!

2 answers

1 accepted

1 vote
Answer accepted
David Fischer
Community Champion
April 29, 2023

Hi @Cailin Che 

this is the code to use to run the JMWE post function (on the Create transition) only for cloned issues:

issue.key != null

However, you need to make sure the post function is placed before the "Creates the issue originally" post function.

The reason this works is that when cloning an existing issue, the issue.key variable is set to the issue key of the source issue, whereas it is empty (null) when creating a new issue from scratch.

Cailin Che
Contributor
May 1, 2023

This did exactly what I need, thank you!

0 votes
Clark Everson
Community Champion
April 28, 2023

Hi @Cailin Che 

I am no expert but you can try this either but often times it's hard to get scripting help on here. You may have better luck reaching out to adaptivist directly. You would want to make a script runner listener and make sure to replace the custom field ids

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

// Add the field IDs you want to clear in the list below
def fieldsToClear = ["customfield_10000", "customfield_10001"]

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def issue = event.issue
def sourceIssue = issueLinkManager.getOutwardLinks(issue.id)?.find {it.issueLinkType.name == "Cloners"}?.sourceObject

if (sourceIssue) {
fieldsToClear.each { fieldId ->
def customField = customFieldManager.getCustomFieldObject(fieldId)
if (customField) {
def changeHolder = new DefaultIssueChangeHolder()
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), null), changeHolder)
}
}

// Reindex the issue after clearing fields
issueManager.reindex(issue)
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.22.6
TAGS
AUG Leaders

Atlassian Community Events