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: https://community.atlassian.com/t5/Jira-questions/How-to-Automatically-create-Sub-task-using-script-runner-post/qaq-p/400564 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>
com.atlassian.jira.exception.CreateException
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:588)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:494)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssueObject(DefaultIssueManager.java:599)
	at com.atlassian.jira.issue.managers.RequestCachingIssueManager.createIssueObject(RequestCachingIssueManager.java:198)
	at com.atlassian.jira.issue.IssueManager$createIssueObject.call(Unknown Source)
	at Script33.run(Script33.groovy:69)
Caused by: com.atlassian.jira.workflow.WorkflowException
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:768)
	at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:580)
	... 5 more
Caused by: java.lang.NullPointerException
	at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:721)
	... 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) {
return
}

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

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

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

// Create new therapies
newTherapies.each {
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setSummary((String)it)
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(constantsManager.getAllIssueTypeObjects().find{
it.getName() == "Therapy"
}.id)

log.info("New issue ${newSubTask}")
def newIssueParams = ["Issue" : newSubTask] as Map<String, Object>
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, user)
log.info("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?

Cheers!

Dyelamos

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,173 views 13 19
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot