It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Clearing fields on cloned issues during cloning action

Joe Pitt Community Leader Jan 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?

9 answers

1 accepted

2 votes
Answer accepted
Joe Pitt Community Leader Jan 11, 2012

I tested clearing the fields and it works.

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.

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.

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

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

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

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.

@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

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

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.

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

Hi @Amir Katz @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!!!

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

Hi @Amir Katz

 

Got it. Thank you very much for your reply

JIRA Clone-Plus plugin supports excluding fields from cloning.

That is true but it is not a free option.

>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.

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

Ignore that, found it. Need coffee badly.

Like David Mayer likes this

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

Any idea why ?

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.

@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

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

Suggest an answer

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

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

841 views 5 22
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you