How to create a subtask using script runner listener (groovy script)? Edited

We are working on using a script listener to automatically generate sub-tasks based on the values in a custom field. I've tried the solution based on this: but it's throwing the following error:

2018-01-08 03:20:27,207 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssueObject(
	at com.atlassian.jira.issue.managers.RequestCachingIssueManager.createIssueObject(
	at com.atlassian.jira.issue.IssueManager$ Source)
Caused by: com.atlassian.jira.workflow.WorkflowException
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(
	... 5 more
Caused by: java.lang.NullPointerException
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(
	... 6 more

And here's my code:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.config.SubTaskManager
import org.apache.log4j.Logger
import org.apache.log4j.Level

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def constantsManager = ComponentAccessor.getConstantsManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

MutableIssue issue = (MutableIssue) event.issue
def subTasks = issue.getSubTaskObjects()
def existingTherapies = []
def newTherapies = []

// Functions
def parseTherapySummary(String str) {
String result = str.substring(str.lastIndexOf(": ") + 2)
return result

// Custom fields
CustomField enrollmentTherapies = customFieldManager.getCustomFieldObjectByName("Enrollment Therapies")
Collection enrollmentTherapiesValue = (Collection)issue.getCustomFieldValue(enrollmentTherapies)

// If no enrolled therapies, exit script and do nothing
if (enrollmentTherapiesValue == null) {

// Generate a list of existing therapies
for (Issue st : subTasks) {
String subtaskSummary = parseTherapySummary(st.summary)

// Check each enrollment therapy see if it already exists
for (String et: enrollmentTherapiesValue) {
if (!(et in existingTherapies)) {

if (newTherapies.size() == 0) {

// Create new therapies
newTherapies.each {
MutableIssue newSubTask = issueFactory.getIssue()
it.getName() == "Therapy"
}.id)"New issue ${newSubTask}")
def newIssueParams = ["Issue" : newSubTask] as Map<String, Object>
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, user)"Issue with summary ${newSubTask.summary} created.")

 Can someone help me understand why this piece of code won't work in the listener?

1 answer

0 votes

你好 Hanxiong!

What version of scriptrunner are you using? We have a builtin version of this postfunction that shouldn't give you any problems. You can see a picture of where it is here:

Screen Shot 2018-01-17 at 11.51.49.pngAre you aware that this is built-in for you? 

If this solved your answer, please accept the answer so that other users know that this question has been solved.

May I help you further?



Suggest an answer

Log in or Sign up to answer
Community showcase
Published Jan 08, 2019 in Jira

How to Jira for designers

I’m a designer on the Jira team. For a long time, I’ve fielded questions from other designers about how they should be using Jira Software with their design team. I’ve also heard feedback from other ...

1,102 views 4 9
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