ScriptRunner how to clone issue with all custom fields Edited

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

1 answer

1 accepted

2 votes

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

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

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

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

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.

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.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

136 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you