Failing to create Sub-task in Clone-issue post-function

Hello,

I am using the Script runner Clone-issue post-function, and in additional actions I want to create Sub-tasks using the contents of a Checkbox. The script works in a "normal" script-runner post-function, but as part of the clone-issue post-function it fails with the following log:

2016-09-27 17:03:03,221 ERROR [utils.ConditionUtils]: Clone issue id = null
2016-09-27 17:03:03,224 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-09-27 17:03:03,224 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: CR-17, actionId: 121, file: null
java.lang.IllegalArgumentException: Parent issue cannot have a null ID.
	at com.atlassian.jira.issue.IssueImpl.setParentObject(IssueImpl.java:964)
	at com.atlassian.jira.issue.MutableIssue$setParentObject$4.call(Unknown Source)
	at Script322$_run_closure1.doCall(Script322.groovy:41)
	at Script322.run(Script322.groovy:37)

For some reasons, the cloned issue has no id, which have logged (as you can see)

Here is the script:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.project.Project
import groovy.transform.Field
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueFactory
@Field
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
@Field MutableIssue cloneIssue = issue
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//name of the custom field
def customFieldName = 'Create necessary documentation'
//get the custom field object
def customFieldObject = customFieldManager.getCustomFieldObjectByName(customFieldName)
 
//get all the values of the custom field
def customFieldValues = customFieldObject.getValue(sourceIssue)
log.error "Clone issue id = " + issue.id
//loop through the values and create sub tasks
//change the case value as per requirements such as "Email Required" or "SAP Access Required"
customFieldValues.each { LazyLoadedOption it ->
    def optionValue = it.getValue()
    MutableIssue newSubTask = issueFactory.getIssue()
    newSubTask.setSummary(optionValue)
    newSubTask.setParentObject(cloneIssue)
    newSubTask.setProjectObject(cloneIssue.getProjectObject())
    newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
        it.getName() == "Sub-task"
    }.id)
    // Add any other fields you want for the newly created sub task
    log.debug("New issue ${newSubTask}")
    Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
    issueManager.createIssueObject(user, newIssueParams)
    subTaskManager.createSubTaskIssueLink(cloneIssue, newSubTask, user)
}

Any clue what I am doing wrong?

1 answer

1 accepted

Hi Mads,

So at that time the clone Issue is not created and thus the id is null. Therefore there is callback 

doAfterCreate = {
//do things after the issue creation, like create subtasks
}

Is not yet documented but it will be soon (if you use one of the SR 4.* versions)

Also a link to a similar question may help you more https://answers.atlassian.com/questions/39558461

Please let me know if this works for you.

regards, Thanos

Thanks, that did it!

UPDATE:

Link to the updated documentation After Create actions

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

43 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you