Script Runner - Create issue in another project based on dropdown list selection Edited

 On the issue creation page, I have a customize field as below

drop-down list A: with value 1, 2, 3,4,5

If 1 was selected, after click save issue create button, clone save issue to project 1, and link these two issues.

If 2 was selected, after click save issue create button, clone save issue to project 2, and link these two issues.

If 3 was selected, after click save issue create button, clone save issue to project 1, and link these two issues.

If 4 was selected, after click save issue create button, clone save issue to project 3, and link these two issues.

If 5 was selected, after click save issue create button, clone save issue to project 1, and link these two issues.

 

import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.atlassian.crowd.embedded.api.User;
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.IssueFactory;

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.util.UserManager

class TaskVersionListener extends AbstractIssueEventListener {
Category log = Category.getInstance(TaskVersionListener.class)

@Override
void workflowEvent(IssueEvent event) {
log.setLevel(org.apache.log4j.Level.DEBUG)
def issue = event.getIssue()
if ( issue.getIssueTypeId() == "XXXXX" ) { // The Issue Type to catch in the Listener
def projectName = "YYYYY" //Target project name

def issueMgr = ComponentAccessor.getIssueManager()
def projectMgr = ComponentAccessor.getProjectManager()
String currentUser = event.getUser().getName();
User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentManager componentManager=ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
log.debug "IF ISSUE IS TYPE XXXXX CLONE ISSUE TO PROJECT YYYYY WITH SAME FIELDS, ETC"

def issueFactory = ComponentAccessor.getIssueFactory()
def newissue = issueFactory.getIssue()
newissue.setSummary (issue.summary)
newissue.setProject (projectMgr.getProjectByName(projectName))
newissue.setIssueTypeId("JJJJ") //The ID of the target Issue Type
newissue.description = issue.description
newissue.reporter = issue.getReporter()

//Set the Project Lead of the target project as assignee of the new ticket
def project = projectMgr.getProjectObjByName(projectName)
newissue.setAssigneeId(project.getLeadUserKey())

//Copy the fields that you want
def cf = null;
//ZZZZZ (list)
cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'ZZZZZ'}
newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
//YYYYYY (date) another way to obtain the custom field
cf = customFieldManager.getCustomFieldObject("customfield_NNNNN");
newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
//XXXXXX (text, numeric, ...)
cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'XXXXX'}
newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))


def params = ["issue":newissue]
def subTask = issueMgr.createIssue(currentUserObj, params)


def linkMgr = ComponentManager.getInstance().getIssueLinkManager()
linkMgr.createIssueLink (newissue.id, issue.id, Long.parseLong("ZZZZZ"),Long.valueOf(1), currentUserObj) //ZZZZZ is the id of the type of link
}
}
}

 

1 answer

0 votes

Hi!

Sorry but I see no question here. Is the script not working?

Cheers!

Dyelamos

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,322 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you