Unable to set 'Epic Link' with Groovy script

Miroslav Kralik March 7, 2018

Hi, 

I am trying to create a task automatically on Epic creation, but I am unable to set 'Epic Link' for the task - no link is added and I am getting also issues that there is an error with the links - no able to view the task, or delete etc - Integrity Checker usually fix this problem (delete any inconsistent links in background - "ERROR: The following Issue Link will be removed due to a related invalid issue: IssueLink (ID:111345)"). 

JIRA: v7.3.6

ScriptRunner: v5.2.2

 

Any idea how to make this working and without issues? 

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.opensymphony.workflow.WorkflowContext

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
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.issue.Issue

import org.apache.log4j.Logger
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.fields.CustomField
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()

IssueManager issueMgr = ComponentAccessor.getIssueManager()
ProjectManager projectMgr = ComponentAccessor.getProjectManager()
CustomFieldManager cfMgr = 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()

// 1. INITIATION
// #1
MutableIssue newissue1 = issueFactory.getIssue()

newissue1.setSummary("Summary and business impact")
newissue1.setProjectObject(project)
newissue1.setIssueTypeId("3") // Task
newissue1.description = "Summary 1"
newissue1.reporter = issue.getReporter()

// Set component
ProjectComponentManager projectComponentManager = ComponentAccessor.getProjectComponentManager();
ProjectComponent projectComponent = projectComponentManager.findByComponentName(project.getId(), "1. INITIATION");
Collection<ProjectComponent> myComponents = new HashSet<ProjectComponent>()
myComponents.add(projectComponent)
newissue1.setComponent(myComponents)

// Set 'Epic Link'
def epicLinkField = cfMgr.getCustomFieldObjects(newissue1).find {it.name == 'Epic Link'}
newissue1.setCustomFieldValue(epicLinkField, issue)

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

ImportUtils.indexIssues = wasIndexing

 

1 answer

0 votes
Kyle Moseley
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 8, 2018

Try performing this after issue creation, updating the already existing issue:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def epicField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Epic Link")
epicField.updateValue(null, newIssue, new ModifiedValue(newIssue.getCustomFieldValue(epicField), issue),new DefaultIssueChangeHolder())
Miroslav Kralik March 8, 2018

Same issues:

- Epic is not set for the task

- Integrity Checker -> ERROR: The following Issue Link will be removed due to a related invalid issue: IssueLink (ID:111345)

ioann god June 10, 2018
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.user.ApplicationUser  

// func
def
 linkIssueToEpic(MutableIssue issue, Issue epicIssue, ApplicationUser user)
{
    def customFieldManager = ComponentAccessor.customFieldManager
    def issueManager = ComponentAccessor.issueManager
    def epicLinkField = customFieldManager.getCustomFieldObjects(issue).find { it.name == 'Epic Link' }
issue.setCustomFieldValue(epicLinkField, epicIssue)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

 

 

And, you must have _Epic Link_ filend in the project screens...

Richard Bone August 7, 2019

Hi,

I'm working in Jira 8.2.1 and using JMWE add-on's Create/Clone issue post function to a new issue, which is standard issuetype and is created as 'Belongs to' its parent epic. I'm also copying several field values from the epic as part of this post-function. I'm also using ScriptRunner 5.5.9.1-jira8 and added the above script in a post-function directly below creating the new issue. The script runs successfully but when the new issue is created, the Epic Link field is not populated as expected. What am I missing?

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.
August 7, 2019

Why are you not setting the Epic Link field directly in the Create/Clone Issue post-function?

Richard Bone August 7, 2019

Hi David,

I tried adding the Epic Link field in the JMWE list of fields that and it won't populate the Epic Link field in the new issue. All other fields come over fine, just not the Epic Link. Again, the parent is an epic but the new issuetype isn't a story and the link type = Is the epic for. 

epic link issue.png

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.
August 7, 2019

Hi Richard,

if you "copy from current issue" the Epic Link field, that means that the new issue will have the same parent Epic as the current issue (the issue that is being transitioned). Is that what you want?

If what you want instead is for the new issue to have the current issue as its parent, you need to use the "Set value to Groovy Expression" option with the following value script:

issue.key

As for the "Is the epic for" link type, I assume it's a custom issue link type you created yourself, as it is certainly not the standard Issue/Epic system link type (which you cannot use in the Create Issue post-function - you need to fill in the Epic Link instead).

Richard Bone August 8, 2019

Hi David,

Yes, the new issue will have the Epic as its parent. As mentioned above, I'm using the Jira Misc Workflow Extensions app which provides the link type. However, after discussing this issue further with my internal customer team, they've decided that, if necessary, the Epic Link field can be populated manually after the new issue has been created. Thanks for your support ... much appreciated!!

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.
August 8, 2019

Hi Richard,

Maybe I wasn't clear, but you can do that from within the Create Issue post-function, as explained in my previous answer. No need for a ScriptRunner script or any other post-function - and certainly no need for manual input. 

Suggest an answer

Log in or Sign up to answer