I have a custom scripted field that holds the project ID of multiple selected projects.
I want to clone an issue to each of those selected project IDs after it is created.
Can someone help me with this?
Funny I was just working on solving a similar problem yesterday.
It's easy enough to use the Canned Post Function "clone issue and link", but that would have to be repeated manually for each project etc.
So we need a way to call that post function n number of times.
One challenge that comes with that, is if you have to create many issues. The user may have to wait a while.
Here is the script I created (adapted for your situation somewhat) that calls the CloneIssue canned script programmatically. This is done in an "each" loop for each project in your projects field.
And to make we don't wait for the creation to finish before doing the next one, we do each of them in a separate thread.
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.Issueimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.issue.issuetype.IssueTypeimport com.atlassian.jira.issue.link.IssueLinkTypeimport com.atlassian.jira.issue.link.IssueLinkTypeManagerimport com.atlassian.jira.project.Projectimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.jira.util.thread.JiraThreadLocalUtilimport com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssueimport com.onresolve.scriptrunner.canned.jira.utils.ConditionUtilsimport com.onresolve.scriptrunner.runner.ScriptRunnerImplimport groovy.transform.Fieldissue = issue as MutableIssuedef currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUserdef PROJECT_CF_NAME = 'CUSTOM FIELD THAT CONTAINS PROJECTS'def TARGET_ISSUE_TYPE_NAME = 'YOUR ISSUE TYPE NAME'def TARGET_LINK_TYPE_NAME = 'YOUR LINK TYPE NAME'def projectsCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName(PROJECT_CF_NAME)[0]def targetIssueType = ComponentAccessor.constantsManager.allIssueTypeObjects.find { it.name == TARGET_ISSUE_TYPE_NAME }def targetLinkType = ComponentAccessor.getComponent(IssueLinkTypeManager).getIssueLinkTypesByName(TARGET_LINK_TYPE_NAME)[0] //this assume a single link type has this namedef projectIds = issue.getCustomFieldValue(projectsCf) as List<Long>projectIds.each { projectId -> def project = ComponentAccessor.projectManager.getProjectObj(projectId) Thread.start { def jiraThreadLocalUtil = ComponentAccessor.getComponent(JiraThreadLocalUtil) jiraThreadLocalUtil.preCall()try { ComponentAccessor.jiraAuthenticationContext.loggedInUser = currentUserdef cloneIssue = cloneIssue(issue, project, targetIssueType, targetLinkType) } catch (e) { log.error "Error in cloning thread for $project.key: $e" } finally { jiraThreadLocalUtil.postCall(log) }}}Issue cloneIssue(MutableIssue originalIssue, Project targetProject, IssueType targetIssueType, IssueLinkType linkType) {def additionalScript = """\ /** * Put your script in here. Default binding variables don't seem to be available. * You can inject them yourself here. For example: */ import com.atlassian.jira.component.ComponentAccessor def originalIssue = ComponentAccessor.issueManager.getIssueObject(${originalIssue.id}) """.stripIndent()def params = [(CloneIssue.FIELD_TARGET_PROJECT) : targetProject.key, (CloneIssue.FIELD_TARGET_ISSUE_TYPE) : targetIssueType.id, (CloneIssue.FIELD_COPY_FIELDS) : CloneIssue.COPY_ALL_FIELDS, (CloneIssue.FIELD_SELECTED_FIELDS) : null, (CloneIssue.FIELD_USER_KEY) : null, (ConditionUtils.FIELD_ADDITIONAL_SCRIPT): [additionalScript, ""], (CloneIssue.FIELD_LINK_TYPE) : linkType.id as String,] as Map<String, Object>def executionContext = [issue: originalIssue] as Map<String, Object>def cloneIssueAction = ScriptRunnerImpl.scriptRunner.createBean(CloneIssue)def updatedExecutionContext = cloneIssueAction.execute(params, executionContext) updatedExecutionContext?.newIssue as Issue}
It looks like you're new here. Sign in or register to get started.