Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set issue link from subtask to parent's epic in post-creation script

Ian Balas September 23, 2021

Hi all,

Is it possible to automatically set an issue link between a sub-task and the epic of the sub-task's parent issue?

Referencing from the Add or Update the Issue Link for an Issue in Jira snippet from Adaptavist Library, I figured I could tweak it and set the source issue as the sub-task that gets created in a 'Create / Clone Issue(s)' post-function, while also setting the destination issue as the (newly-created) sub-task's parent issue's epic. Please see below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager

def issueManager = ComponentAccessor.issueManager

// the issue key to create the link from
final String sourceIssueKey = newIssue.getAsString("issuekey")

// the issue key to create the link to
final epicLinkFieldName = "customfield_10001"
def epicLinkField = customFieldManager.getCustomFieldObject(epicLinkFieldName)
final String epicIssueKey =
issueManager.getIssueObject((String)issue.getParentObject().getCustomFieldValue(epicLinkField))

// the name of the issue link
final String issueLinkName = "Blocks"

// the sequence of the link
final Long sequence = 1L

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)

def sourceIssue = issueManager.getIssueByCurrentKey(sourceIssueKey)
def destinationIssue = issueManager.getIssueByCurrentKey(epicIssueKey)
assert sourceIssue && destinationIssue: "Source and/or Destination Issue(s) does not exist"

def availableIssueLinkTypes = issueLinkTypeManager.issueLinkTypes
def linkType = availableIssueLinkTypes.findByName(issueLinkName)
assert linkType : "Could not find link type with name $issueLinkName. Available issue link types are ${availableIssueLinkTypes*.name.join(", ")}"

ComponentAccessor.issueLinkManager.createIssueLink(sourceIssue.id, destinationIssue.id, linkType.id, sequence, loggedInUser)

This isn't working, unfortunately. I seem to get an error while running the script indicating: "groovy.lang.MissingPropertyException: No such property: newIssue for class: script_ef92563cb0cb30c482015f71558b3876"

I'm not sure why I'm getting this error since I'd have to use the newIssue variable to get the issue key of the sub-task created by this function (right?).

0 answers

Suggest an answer

Log in or Sign up to answer