Clone a Task from an Epic, then link it via Epic link using scriptrunner

Shawn Danisa August 8, 2019

I would like to automatically create a task from by cloning an Epic. then link the created Task to the Epic. The task is created successfully, however, the Epic linking is failing. 

Kindly assist:

 

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.util.ImportUtils
import com.opensymphony.workflow.WorkflowContext

import org.apache.log4j.Logger
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)

// Get project object
Project project = issue.getProjectObject()

def IssueManager issueMgr = ComponentAccessor.getIssueManager()
ProjectManager projectMgr = ComponentAccessor.getProjectManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()


// Get user object
String currentUser = (String) ((WorkflowContext) transientVars.get("context")).getCaller();
ApplicationUser currentUserObj = ComponentAccessor.getUserManager().getUserByKey(currentUser);

def wasIndexing = ImportUtils.indexIssues
ImportUtils.indexIssues = true
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()

// Epic attributes
def epicSummary = issue.getSummary()
def key = issue.getKey()
def epicIssue = issueMgr.getIssueObject(key)
def epicType = customFieldManager.getCustomFieldObject("customfield_14401")
def epicTypeValue = issue.getCustomFieldValue(epicType)

// Create issue
MutableIssue newIssue = issueFactory.getIssue()

if(epicTypeValue == "Planning")
{
newIssue.setSummary("Planning: " + epicSummary)
newIssue.setProjectObject(project)
newIssue.setIssueTypeId("10200") //TASK type
newIssue.description = "-"
newIssue.reporter = issue.getReporter()

def epicLink = customFieldManager.getCustomFieldObjects(newIssue).find { it.name == 'Epic Link' }
newIssue.setCustomFieldValue(epicLink, epicIssue)

// Create Issue
issueMgr.createIssueObject(currentUserObj, newIssue)

}

ImportUtils.indexIssues = wasIndexing

0 answers

Suggest an answer

Log in or Sign up to answer