Clearing fields on cloned issues during cloning action

Joe Pitt
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 11, 2012

We have some custom fields that capture date/time and user that executed some transitions. when cloning an issue these fields are also cloned. If I put a post function on the Create event in the workflow for that issue type to clear them will that be executed when the issue is cloned or is another create process used when cloning?

12 answers

1 accepted

2 votes
Answer accepted
Joe Pitt
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 11, 2012

I tested clearing the fields and it works.

Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 11, 2012

Will that not also clear the fields when user creates an issue and fills in those fields? Oh, but, I see you are only talking about transition specified fields.

2 votes
Dana Frost March 17, 2012

Unfortunatly, this will also clear the value that the user enteres on a Create so this is not acceptalbe for my use. For example, the field that I want to clear is a "Screen" flag which indicates an issue has been screened. But ofen-times, managers want to mark an issue screened when they create it. So, using a POST function on the create will clear this for both Create and Clone which is no good.

If you never want to set the field during a create step, I suppose it can work but not if you want to control just the Clone operation. I am still looking for a way to do this.

It seems odd that the "Create" action is only accesable from the Design screen and not the "Steps" workflow edit screens but it did work for me. I was able to clear a number of Custom fields (using JiraSuiteUtils) in the create action.

1 vote
Amir Katz (Outseer)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 26, 2019
Amir Katz (Outseer)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 26, 2019

I've created a post-function that implements Thanos' suggestion from the link I shared above.

Some observations:

1. It works (duh!)

2. In the post-functions definition, the script must be placed AFTER the action 'Create the issue originally'.

3. For debug, I pop up a user message box. It shows up in issue creation, but not during issue cloning. This is weird, but the code works.

HTH, Amir

Muface Calidad March 29, 2019

Hi Amir,

I would appreciate your full script doing that. My script apparently works fine but when i see, the script has not done anything.

Thanks in advance,

Regards

Amir Katz (Outseer)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 30, 2019

Here it is:

 

// Note: Not sure whether all imports are required, it's a cut & paste from other scripts

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.issue.IssueEventManager
import com.atlassian.jira.event.issue.IssueEventBundleFactory
import com.atlassian.jira.event.issue.IssueEvent
import webwork.action.ActionContext
import java.time.*
import java.time.format.DateTimeFormatter
import com.onresolve.scriptrunner.runner.util.UserMessageUtil


def debugMode = false as Boolean
def msgDebug
final String scriptName = "CCB-Post-01: "
DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern(" @ yyyy-MM-dd HH:mm:ss ")
def dateTimeNow

dateTimeNow = LocalDateTime.now().format(dtFormatter)

def is_cloner = false

def request = ActionContext.getRequest()
if (request) {
msgDebug = scriptName + "Issue was created manually" + dateTimeNow + "[D03]"
UserMessageUtil.info(msgDebug)
} else {
msgDebug = scriptName + "Issue was created via CLONE action" + dateTimeNow + "[D04]"
UserMessageUtil.info(msgDebug)
is_cloner = true
}

if (!is_cloner) {
return true
}

def changeHolder = new DefaultIssueChangeHolder()
def issueManager = ComponentAccessor.getIssueManager()

def Reviewed_by_CF = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_12345")
def Reviewed_by_Value = issue.getCustomFieldValue("customfield_12345")

// Clear custom field value
Reviewed_by_CF.updateValue(null, issue, new ModifiedValue(Reviewed_by_Value, null), changeHolder)
// Update issue and fire "Issue Updated" event
issueManager.updateIssue(null, issue, EventDispatchOption.ISSUE_UPDATED, false)

true
Muface Calidad April 2, 2019

Thanks Amir,

We tried your solution but it didn't works.

Finally we decided to create a Script Runner listener (Clones an issue, and links), configuring in it the parameters we want to clone.

Actually it's way more simple than post-function scripts and  easier to manage.

Anyways, thank you for your help.


Regards.

Amir Katz (Outseer)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 2, 2019

@Muface Calidad 

1. Can you please let me know what does not work? It does work for me, but no 2 Jira systems in the world are identical :-)

2. The listener listens on which event?

3. Can you share the code of the listener?

Thanks

Muface Calidad April 2, 2019

I am actually working on the listener solutions (we have faced some issues).

About your solution, i haven't do nothing special. I just simply copy your script and put in the post-function second step (after Creates the issue originally).

Only copying the script, the console shows an error in the next line:

def Reviewed_by_Value = issue.getCustomFieldValue("customfield_10008")

"Cannot find matching method ... com.atlassian.jira.issue.MutableIssue#getCustomFieldValue"

Our JIRA version is 

  • v7.3.3

Our plugin version is 

  • 4.3.13

If i try to run it with the error, the script fails in execution showing the next message:

groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script335
 at Script335.run(Script335.groovy:58)

Thanks

Amir Katz (Outseer)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 2, 2019

My bad, I omitted this line:

MutableIssue issue = issue

 

which should be placed somewhere after all the import statements.

Let me know if it works.

Muface Calidad April 2, 2019

Finally works!!! 

The only problem i had since the start was the issue.store() tag. Without this tag, the update didn't was save after the change. We add it at the end of the script.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.event.type.EventDispatchOption
import webwork.action.ActionContext
import com.onresolve.scriptrunner.runner.util.UserMessageUtil


def debugMode = false as Boolean
def msgDebug

def is_cloner = false

def request = ActionContext.getRequest()
if (request) {
msgDebug = "Issue was created manually"
UserMessageUtil.info(msgDebug)
} else {
msgDebug = "Issue was created via CLONE action"
UserMessageUtil.info(msgDebug)
is_cloner = true
}

if (!is_cloner) {
return true
}

def changeHolder = new DefaultIssueChangeHolder()
def issueManager = ComponentAccessor.getIssueManager()
def Reviewed_by_CF_EndDate = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10008")
def Reviewed_by_Value_EndDate = issue.getCustomFieldValue(Reviewed_by_CF_EndDate)

def Reviewed_by_CF_StartDate = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10007")
def Reviewed_by_Value_StartDate = issue.getCustomFieldValue(Reviewed_by_CF_StartDate)

Reviewed_by_CF_EndDate.updateValue(null, issue, new ModifiedValue(Reviewed_by_Value_EndDate, null), changeHolder)
Reviewed_by_CF_StartDate.updateValue(null, issue, new ModifiedValue(Reviewed_by_Value_StartDate, null), changeHolder)

issueManager.updateIssue(null, issue, EventDispatchOption.ISSUE_UPDATED, false)
issue.store()
true 


I decided to use it via a Custom listener (Create Issue Event). Actually, this listener just acts like the post-function but it's way more manageable.

I appreciate your help.

Regards

Like # people like this
parthiban_selvaraj October 13, 2019

Hi @Amir Katz (Outseer) @Muface Calidad ,

 

Do any one understand how the script is working ? I wonder how it works perfectly, from my understanding none of the line is checking whether it is cloning or creation ?

It clearing the field while cloning and creation it leaves as it is.

Can Any one explian how the above code works please.

 

CC @Joe Pitt @Thanos Batagiannis _Adaptavist_ 

 

Regards!!!

Amir Katz (Outseer)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 19, 2019

The script relies on the hack (suggested earlier in this thread) that assumes that the function

ActionContext.getRequest()

returns non-null on user-initiated create action and null on clone. 

Amir

Like parthiban_selvaraj likes this
parthiban_selvaraj October 19, 2019

Hi @Amir Katz (Outseer)

 

Got it. Thank you very much for your reply

0 votes
Bill Noble April 14, 2023

Here is the script that I use to clear a field when an issue of a specific type (in this case Epic) is cloned:

/*
This example ScriptRunner custom listener script must be configured to fire
on the 'IssueLinkCreatedEvent' Event. It shows how when an issue is cloned
that you can clear the value of a custom field on the cloned issue. Closely
based on: https://community.atlassian.com/t5/Jira-questions/I-want-to-clear-the-field-value-during-the-clone-of-the-issue/qaq-p/1550276
*/

// This Script Listener should be configured to run on the Issue Link Created Event
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

String ISSUE_TYPE_ID = "6" // ID for an Epic
Long CUSTOM_FIELD_ID = 11406 // ID for the custom field (a checkbox)

public class IssueCreatedResolvedListener {
private static final Logger log = LoggerFactory.getLogger(IssueCreatedResolvedListener.class);
}

// Check if the type of the issue link type of the issue is 'Cloners' which is the issue link type that Jira uses when cloning an issue.
// If it is a cloned issue of type Epic, then perform the logic to clear field values
IssueLink issueLink = event.getIssueLink()
if (issueLink.issueLinkType.name == "Cloners"){
MutableIssue issue = issueLink.getSourceObject()

if (issue) {
if (issue.getIssueType().getId() != ISSUE_TYPE_ID) {
//log.info("The issue is not an Epic, so skipping")
return
}

log.info("The Epic has been cloned, so now removing the values from the fields which should not be set")

IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField textCustomField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID)
issue.setCustomFieldValue(textCustomField, null)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

// If the issue is not a clone, then log a message saying this and do nothing
}else{
//log.info("The issue has not been cloned so do nothing")
}
0 votes
Jonas Frid December 11, 2020

I had a similar need which I found only parts of solution of (by @Thanos Batagiannis). I thought someone else might want it soon enough and will stumble on this precise place where my research begun.

Additional problem I had was that I needed to set my custom field to it's default value, not only clear it, since one configuration have one of a few checkboxes checked as default value.

This implementation works when added as Scriptrunner Custom Script postfunction in my Create transition. Jira Server 8.13.1

//This post function must(?) be placed before post function "Creates the issue originally."

//***Perform tasks when cloning***
import com.atlassian.jira.component.ComponentAccessor
//Bunch of includes that might be useful for other Custom Field Types ... or not?
//import com.atlassian.jira.issue.fields.CustomField
//import com.atlassian.jira.issue.IssueManager
//import com.atlassian.jira.issue.MutableIssue
//import com.atlassian.jira.issue.ModifiedValue
//import com.atlassian.jira.issue.customfields.manager.OptionsManager
//import com.atlassian.jira.issue.fields.config.FieldConfig
import webwork.action.ActionContext

//Get custom field
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectsByName("Relaxing name")[0]

//Get field config and custom field type default value
//https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-get-custom-field-s-default-value-for-validator-script/qaq-p/506707
def fieldConfig = cf.getRelevantConfig(issue)
def cfType = customFieldManager.getCustomFieldTypes().find {it.name == "Checkboxes"}
def rnDefault = cfType?.getDefaultValue(fieldConfig)

log.debug("Default value : ${cfType?.getDefaultValue(fieldConfig)}")

//Initial creation results in request, Cloning doesn't. Ugly but seams to be the way to do this.
//https://community.atlassian.com/t5/Answers-Developer-Questions/Condition-to-check-if-issue-is-cloned-vs-initial-creation-to/qaq-p/570243
def request = ActionContext.getRequest()
if (request) {
//issue.setCustomFieldValue(cf, rnDefault)
log.debug("Manually Created issue")
return true
} else {
issue.setCustomFieldValue(cf, rnDefault)
log.debug("Cloned issue")
return true
}
Karthick Mohan December 17, 2020

What is the post-function name to add this script?

Laci February 9, 2021

@Jonas Frid I tried your script and it fails based on this:

Cannot invoke method getRelevantConfig() on null object

 

Thoughts?

0 votes
JacksonC January 17, 2020

Hello, 

When doing a Clone to another project, are you able to get the project key of the new project being cloned into?

I've been searching and testing all day.

issueContext.projectObject.key  - Always returns the key of the issue being cloned from.

I tried variations of getFieldByName and getFieldByID with project/projects/Project/Projects and it always returns nulls.

Thanks in advance.

Amir Katz (Outseer)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 18, 2020

Hey @JacksonC , I suggest you post a new question to the forums (with a new subject line) as this thread has been answered.

I am interested in the answer...

Amir

Amir Katz (Outseer)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 18, 2020

Try this and let me know if it works:

import com.atlassian.jira.project.Project


Project project = issue.getProjectObject()
def projectKey = project.getKey()

def projectManager = ComponentAccessor.getProjectManager()
def projectObj = projectManager.getProjectObjByKey(projectKey)

JacksonC January 19, 2020

Thank you for your suggestion. I just tried it and it did not work.

It still gets the project key of the project being cloned from.

I've posted a new question here: https://community.atlassian.com/t5/Jira-questions/How-to-get-new-project-key-on-Clone-Plus-plugin-with-Behaviours/qaq-p/1274969#U1275237

I've also opened a support ticket with Clone Plus.

I'll note down in the post above what I find.

0 votes
KoteswaraRao Gudla - Consultant December 21, 2018

Hi Guys,

I am facing same issue too, (field getting cleared soon after issue is created).

Is there any way we can accomplish this (without using clone+ ) in the ways like re-ordering post functions

0 votes
raja shekar December 23, 2016

@Joe Pitt I have a same requirement to unset assignee when you clone but i don't see "Clear Field Value" in post function. Can you please help?


error.JPG

0 votes
Brian Spence
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 8, 2015

It also doesn't work for Read-Only fields.

 

Without paying for an add-on, or doing risky database modifications, JIRA does not appear to provide a solution to this problem.

0 votes
EL March 23, 2014

Clear value (post-function) is works great on custom fiels but not on system fields LABELS.

Any idea why ?

0 votes
François Manchon January 16, 2012
>put a post function on the Create event in the workflow 
>for that issue type

This works for me. I added a post function "clear issue field" into the "Create" transition.
Now when I clone an issue the field is cleared.

Note: the field is also cleared when a brand new issue is created. This is fine with my workflow.

Rob Horan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 6, 2015

Dumb question, but how do I edit that? When editing workflows I only see transitions from Open on.

Rob Horan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 6, 2015

Ignore that, found it. Need coffee badly.

Like David Mayer likes this
Rob Horan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 17, 2020

Just coming back to say I still need coffee.

0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 11, 2012

JIRA Clone-Plus plugin supports excluding fields from cloning.

Rob Horan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 6, 2015

That is true but it is not a free option.

Karthick Mohan December 17, 2020

From which version we have this feature to exclude the fields from cloning?

Suggest an answer

Log in or Sign up to answer