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

JIRA Server 7.3.4 with JMWE - Groovy help to copy an issue.

Christa Meck March 21, 2018

Hi Team.  

Scenario:  On transition from New -> Approved, determine if a custom field checkbox is checked.  If checked, clone the ticket to another project and link to this ticket.

Details: JIRA Server 7.3.4 with JMWE 

User level:  I'm a Groovy newb.  I am the JIRA application admin but not the sys admin.

 

This code is getting me a 1 if the checkbox is checked.  So I feel like I am on the right track there.


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObjectsByName("HR: Personnel Change (Needs Facilities)?")
def cFieldValue = issue.getCustomFieldValue(cField).toString();

if (cFieldValue == "[Yes]") { return "1" }
else { return cFieldValue }

As for the copy/linking part...  I've scoured the internet and all I can find are a bunch of code snippets to copy using ScriptRunner.  I can't add additional libraries as I am not the system administrator, just the app admin.  Keep running into class issues and method challenges.  

The most promising code I found is this:

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
}
}
}

 

But my version of JIRA doesn't like those first two imports.  Links, references, anything is helpful.  I'm totally stuck. :(

- Christa

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 21, 2018

Hi Christa,

the code you likely copied from the Internet is meant for ScriptRunner, not JMWE. To check whether a checkbox is checked, see https://innovalog.atlassian.net/wiki/spaces/JMWE/pages/107413622/User+created+custom+fields#Usercreatedcustomfields-Checkboxes/Multi-selectlist

As for copying the issue, you should just use the "Create/Clone Issue" post-function, with the checkbox-checking code as the Conditional Execution script.

Christa Meck March 21, 2018

David,

Thank you for breaking me out of my rut and making me rethink the approach.  Using Create/Clone issue worked perfectly and the link above made my life simpler.  

Seriously.  Thank you.

For anyone curious, my post function looks like this:

Create a new issue:
Project: FAC
Issue type: Room Change
Link type: 5-related task (Related)
Fields:
Summary: Set value from Groovy Template:
Seating Change - <%=issue.get("summary")%>

Run this post-function conditionally.

I'll refine it over time.  :)

- Christa 

TAGS
AUG Leaders

Atlassian Community Events