ScriptRunner how to clone issue with all custom fields

Roberto L
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 21, 2017

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

2 answers

1 accepted

3 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
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 21, 2017

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:

Screen Shot 2017-08-21 at 3.04.47 PM.pngYou can use this built-in script to configure specifically which fields you want to clone:

Screen Shot 2017-08-21 at 3.05.36 PM.png

Roberto L
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 21, 2017

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

Joshua Yamdogo @ Adaptavist
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 21, 2017

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. 

Screen Shot 2017-08-21 at 4.03.40 PM.png

Roberto L
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 21, 2017

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

Joshua Yamdogo @ Adaptavist
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 21, 2017

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.

Qi Jiang October 12, 2017

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.

0 votes
Deleted user November 2, 2018

Joshua Yamdogo @ Adaptavist do you know since which version this is covered as a Build-in-Script? 

 

Thank you in advance

Joshua Yamdogo @ Adaptavist
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.
November 5, 2018

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

Deleted user November 5, 2018

 

Hi Josh, 

found it! Thank you so much!

Best 

Anna

 

Joshua Yamdogo @ Adaptavist
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.
November 5, 2018

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:

Screen Shot 2018-11-05 at 8.38.59 AM.png

Does that make sense?

Regards,

Josh

Deleted user November 5, 2018

I re-read your answer and then it made sense. Thanks so much for your help! :-)

Joshua Yamdogo @ Adaptavist
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.
November 5, 2018

No problem. Thx for using ScriptRunner!

Regards,

Josh

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events