You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
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.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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.