Hello Community,
Would anyone be able to point me in the right direction regarding cloining an issue with all the custom fields found in such issue.
Currently I am doing the following to clone an issue (it works, but lacks alot of information).
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.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.web.bean.PagerFilter
Issue issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_10126")
def cFieldValue = issue.getCustomFieldValue(cField) as String
def PROJECT_KEY_TO = "HTP"
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueFactory = ComponentAccessor.getIssueFactory()
def projectTo = ComponentAccessor.getProjectManager().getProjectByCurrentKey(PROJECT_KEY_TO)
def newIssue = issueFactory.cloneIssue(issue)
newIssue.setProjectObject(projectTo)
Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>;
issueManager.createIssueObject(user, newIssueParams)
UserMessageUtil.success("Issue "+newIssue.getKey()+" cloned to project"+ projectTo.getKey());
The Issue is succesfully cloned but for somereason none of my custom fields are cloned.
I would like to achieve a complete clone that copies over every custom field of the issue.
Any help is greatly appreciated!
-Roberto
Hi Roberto,
What is your use case for this? Are you trying to have an issue cloned on a specific event? Is this script within a listener? Or do you want an issue cloned whenever it is transitioned?
ScriptRunner already has this functionality built-in depending on which verison you're using. You shouldn't really need to write this yourself.
You'll find this button on the both the listeners and script post-functions page:
You can use this built-in script to configure specifically which fields you want to clone:
HI Joshua Yamdogo @ Adaptavist
This helps a lot, i am going to use this.
my Use Case is to clone an issue to another project whenever an issue is updated and it meets certain criterias
I am new to this type of Listener, could you help me figure out how to set my condition for it such that it runs once a certain custom field (which is a checkbox) has a value that contains the string "requires attention".
Thank you for your insight!
-Roberto
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Roberto,
From that use case, that sounds like a perfect fit for this type of listener.
As for the condition, this will work:
'Your value here' in cfValues['Name of your checkbox here']*.value
It's worth noting that if you click the "Expand Examples" below the Conditions box, you will see a lot of really useful conditions. This is the same example condition used for multi-select lists.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Joshua Yamdogo @ Adaptavist,
Thanks for that!
i have one question regarding the condition. When you say name of checkbox is that the customfield name or the actual name that shows in the form? Custom field name is custom_10393 while the actual name shown in the form is Security Impact.
will that condition work even if the user clicks two or more values. For my usecase the user can click all 6 boxes but as long as "requires attention" is selected then it needs to be cloned. Will that condition work for my use case?
Thank you!!!
-Roberto
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Roberto,
You should use the actual name of the custom field. So something like this maybe:
'requires attention' in cfValues['Security Impact']*.value
Yes, the condition will work regardless of how many checkboxes they select. cfValues will return an array of all the options selected. As long as they have the right one selected, the condition will return true because it will be a value within that array.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Joshua,
build-in clone function need select a target project, what is target project is based on a customfield value?
for example:
drop-down list A: with value 1, 2, 3,4,5,6
If 1 was selected, after click save issue create button, clone save issue to project B, and link these two issues.
If 2 was selected, after click save issue create button, clone save issue to project C, and link these two issues.
If 3 was selected, after click save issue create button, clone save issue to project B, and link these two issues.
If 4 was selected, after click save issue create button, clone save issue to project D, and link these two issues.
If 5 was selected, after click save issue create button, clone save issue to project B, and link these two issues.
If 6 was selected, after click save issue create button, clone save issue to project E, and link these two issues.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Joshua Yamdogo @ Adaptavist do you know since which version this is covered as a Build-in-Script?
Thank you in advance
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anna,
The "Clone an issue, and links" built-in script has been apart of ScriptRunner for Jira SERVER for many years. I believe it should be in all versions of ScriptRunner that are version 2.1 or higher.
Thanks,
Josh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Josh,
found it! Thank you so much!
Best
Anna
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Anna,
Sorry for the confusion! Yes, you are correct that it does not appear on the "Built-in scripts" page. I meant to say that the "Clone an issue, and links" is a script that is a built-in functionality for ScriptRunner which you can use when adding a Listener or Post-function.
For example, if you create a new Listener in ScriptRunner, you'll see the script there:
Does that make sense?
Regards,
Josh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I re-read your answer and then it made sense. Thanks so much for your help! :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem. Thx for using ScriptRunner!
Regards,
Josh
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.