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?
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
Our plugin version is
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jonas Frid I tried your script and it fails based on this:
Cannot invoke method getRelevantConfig() on null object
Thoughts?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
>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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dumb question, but how do I edit that? When editing workflows I only see transitions from Open on.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ignore that, found it. Need coffee badly.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just coming back to say I still need coffee.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
JIRA Clone-Plus plugin supports excluding fields from cloning.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That is true but it is not a free option.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
From which version we have this feature to exclude the fields from cloning?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.