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?
你好 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:
Are 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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.