package example.CloneTicketAndLink 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; 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 = ComponentManager.getInstance().getIssueManager() def projectMgr = ComponentManager.getInstance().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 = ComponentManager.getInstance().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 } } }
I am trying the same code in my post function. But it is not working
Kindly help
Regards,
Sid
LikeLiked by 1 person
Hi Siddheshwar, this code is for Jira 6 Server, now in Jira 7 Server the code is different, because there are obsolete methods. See this https://mraddon.blog/2016/12/27/migrating-groovy-scripts-from-jira-6-to-jira-7/
Cheers
LikeLike
Thanks!
I have compatible according to supported API/code
Error has vanished
But, issue clone is not working also link is not appearing is screen
Regards,
Sid
LikeLiked by 1 person
Hi Siddheshwar can you put here your code changed?
LikeLike
Hello,
This article is very helpful.
I am able to create a copy ticket and link, I was wondering how can we copy ticket and create multiple issue types and link it.
Thanks
LikeLike
Sure… Following is the updated code
Note: I am taking Project Name from my custom field “Project(Type-Project Picker)”
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;
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()
def cust_field=ComponentAccessor.getCustomFieldManager();
String sid2= cust_field.getCustomFieldObjectByName(“Project”)
log.error”————-sid2————“+sid2
if ( issue.getIssueTypeId() == “10000” ) { // The Issue Type to catch in the Listener
def projectName = sid2 //Target project name
log.error”————-Project Name————“+projectName
def issueMgr = ComponentAccessor.getIssueManager()
def projectMgr = ComponentAccessor.getProjectManager()
String currentUser = event.getUser().getName();
def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ComponentManager componentManager=ComponentManager.getInstance()
CustomFieldManager customFieldManager = ComponentAccessor.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.setProjectObject (projectMgr.getProjectObjByName(projectName))
newissue.setIssueTypeId(“1000”) //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.createIssueObject(currentUserObj, newissue)
def linkMgr = ComponentAccessor.getIssueLinkManager()
linkMgr.createIssueLink (newissue.id, issue.id, Long.parseLong(“ZZZZZ”),Long.valueOf(1), currentUserObj) //ZZZZZ is the id of the type of link
}
}
}
LikeLike
Hi,
I have tested the above code again, and it is creating Clone of a ticket in other projects.
But “Links” are not coming. Can you help me on that?
Regards,
Sid
LikeLike
Sorry, It is not creating Clone
LikeLike
Take this code (it’s other similar but for Jira 7)
Remember: you must create a folder called “example” in your “scripts” folder
Remember: you must place the file inside the “example” folder with name CloneAndLink.groovy
Remember: you must change/replace the “ID”‘s for your Jira ID’s. i.e. Type of Issuelink, etc.
Let me know if it works 😉
LikeLike