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

Hanxiong Shi January 7, 2018

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
Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 17, 2018

你好 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 Sign up to answer