Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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
4,463,662
Community Members
 
Community Events
176
Community Groups

Clone issue to multiple selected products

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?

1 answer

0 votes

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.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.thread.JiraThreadLocalUtil
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
import com.onresolve.scriptrunner.canned.jira.utils.ConditionUtils
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import groovy.transform.Field

issue = issue as MutableIssue

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def 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 name

def 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 = currentUser
def 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
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events