Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Select project based on custom field during Clone an issue, and links post function (Scriptrunner)


I am using the ScriptRunner "Clone an issue, and links" post function.


I am trying to figure out what I need to add in the "Additional issue actions" section to create the issue in a project based on a project picker custom field. 


E.g. Users can select the project they want to clone to during the transition and the issue will be cloned to that project.

1 answer

0 votes

How can users choose the project? Is it a custom field? This post function already creates an issue. Do you want to create another one?

It is a custom field that users can select on the workflow transition screen. 


I only want to clone the issue once but I want the project key for the cloned issue to be based on the user selection (custom field)

I see. It is not that simple to change the project key. You should not use the built-in function. You should create your own custom script, which would create an issue out of the source issue.

Ok. I have most of that working but I cannot seem to copy the issue comments.


Here is my code so far: 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def issueManager = ComponentAccessor.getIssueManager()
def cField = customFieldManager.getCustomFieldObject("Move to Project")
def originalIssueKey = issueManager.getIssueByCurrentKey(issue.key)
def destinationProjectKey = ComponentAccessor.projectManager.getProjectObjByKey(cField.toString())
def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(originalIssueKey)

cloneIssue(originalIssueKey, destinationProjectKey)
issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
void cloneIssue (MutableIssue issue, Project project) {
def params = [
issue : issue,
(CloneIssue.FIELD_TARGET_PROJECT) : project.key,
(CloneIssue.FIELD_SELECTED_FIELDS) : null, //clone all the fields
] as Map<String, Object>

new CloneIssue().doScript(params)

for (Comment comment : comments)
commentManager.create(originalIssueKey, comment.getAuthorApplicationUser(), comment.getBody(), false);


doAfterCreate seems to be giving me some problems.


Also,  since I am using the same code as the clone issue listener, what should I return so I can reference the new issue in the "doAfterCreate" block?


Lastly, I am running this code as a Scriptrunner action in the Automation Plug-in

I never used ScriptRunner in automation plugin

It is the same. I just lets me run any scriptrunner script I create as part of the automation tool. Think of it as a listener that fires whenever something happens in JIRA.

Phillip, Not sure if you figured this out but if your intent is to copy the comments over you can pass them in the params

def params = [
issue : issue,
(CloneIssue.FIELD_TARGET_PROJECT) : project.key,
**(CloneIssue.FIELD_COPY_COMMENTS) : true, **
(CloneIssue.FIELD_SELECTED_FIELDS) : null, //clone all the fields
] as Map<String, Object>

The doAfterCreate you would use stuff like Linking the two issue together or assigning watchers 

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

217 views 0 6
Join discussion

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