I am creating a post-function that will create a clone and automatically link to the parent on a transition.
Code Below.
This successfully creates the (cloned) issue, however the link is not being created.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.Project
import com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
// get the current list of outwards depends on links to get the sequence number
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
Issue mainIssue = issue
CustomField projectSelected = customFieldManager.getCustomFieldObject("customfield_11918")
String projectSelectedValue = mainIssue.getCustomFieldValue(projectSelected) as String
String actualProjectVal = projectSelectedValue.replaceAll("Project:","")
log.info "Project Selected: "+actualProjectVal
def originalIssueKey = mainIssue
log.info "Original issue Key: " +originalIssueKey
def destinationProjectKey = ComponentAccessor.projectManager.getProjectObjByKey(actualProjectVal.trim())
log.info "Destination Project Key: " + destinationProjectKey
def commentManager = ComponentAccessor.getCommentManager()
cloneIssue(originalIssueKey, destinationProjectKey, log)
issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
void cloneIssue (MutableIssue issue, Project project, Category log ) {
log.info "Project.key = " + project.key
log.info "Issue: " + issue
def params = [
issue : issue,
(CloneIssue.FIELD_TARGET_PROJECT) : project.key,
(CloneIssue.FIELD_SELECTED_FIELDS) : null, //clone all the fields
] as Map<String, Object>
new CloneIssue().doScript(params)
}
def doAfterCreate = {
log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
def issueToLinkTo = issueManager.getIssueByCurrentKey(originalIssueKey.toString().trim())
log.info "Issue to link to: " + issueToLinkTo
def linkTypeName = "Blocks"
def linkType = issueLinkTypeManager.getIssueLinkTypesByName(linkTypeName)
if (!linkType) {
log.warn ("Issue link type with name: ${linkTypeName} does not exist")
}
else {
long sequence = 1
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser()
log.info "Created issue: " + issueManager.getIssueObject(issue.id).toString()
issueLinkManager.createIssueLink(issueToLinkTo.id, issue.id, linkType[0].id, sequence, currentUser)
UserMessageUtil.success('Derive Successful.')
}
}
The one different i notice - is Scriptrunner does not declare the doAfterCreate. Howeve I have.
If I remove the declaration, I get an error that it is undeclared.

Please help.