How do I obtain the new issue's key when using Scriptrunner's 'Clone Issue' example script

Richard Lucas May 16, 2023

I'm attempting to use ScriptRunner to build a REST endpoint to clone issues (as it seems this is not supported in the API as is) so that items can be cloned using the API. 

I have found the example script (https://library.adaptavist.com/entity/basics-clone-issue) will clone the issue, but doesn't return the details of the new item that have been created.

Any ideas how I can get the key of the new item would be appreciated, as I hope then to return that back in the response to the calling script for further use.

1 answer

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 17, 2023

Hi @Richard Lucas

For your requirement, you can use the working sample code below:-

import com.adaptavist.hapi.jira.issues.Issues

final def originalIssueKey = 'MOCK-1'
final def amountField = 'Amount'
final def sampleTextField = 'Sample Text Field'

def originalIssue = Issues.getByKey(originalIssueKey)

def clonedIssue = Issues.create(originalIssue.projectObject.key, originalIssue.issueType.name) {
setSummary(originalIssue.summary)
setDescription(originalIssue.description)
setReporter(originalIssue.reporter)
setCustomFieldValue(amountField, originalIssue.getCustomFieldValue(amountField).toString())
setCustomFieldValue(sampleTextField, originalIssue.getCustomFieldValue(sampleTextField).toString())

if (originalIssue.assignee) {
setAssignee(originalIssue.assignee)
}

if (originalIssue.components.size() > 0) {
def components = originalIssue.components.collect { it.name}.toArray() as String[]
setComponents(components)
}
}

if (originalIssue.comments) {
originalIssue.comments.each {
clonedIssue.addComment(it.body)
}
}

log.warn "====== Cloned Issue Key ${clonedIssue.key}"

Please note that the working sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

At the very bottom of the code, you will see there is a log.warn added, this will print out the Cloned Issue's key.

Please also ensure that you upgrade your ScriptRunner plugin to the latest release, i.e. 8.2.1, so you can use the HAPI feature as shown in the sample above to simplify the code.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 24, 2023

Hi @Richard Lucas

Does the code provide the answer to your question?

If yes, please accept the solution.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer