How can I generate multiple sub-tasks as a Groovy Post Function of a Create Transition

Jordan Klein December 19, 2017

When a user creates an issue that with the summary "Markup Master Task - X issues", I'd like to automatically create X number of SubTasks.

ScriptRunner has an option to create a single sub-task on a transition, but I'm looking for something to create multiple sub-tasks rather than manually add a bunch of separate Post Functions.

Here's what I have so far as a Custom Script Post Function for a Create Issue transition. Any help is appreciated!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def parentIssue = sourceIssue
int numMarkups

doAfterCreate = {

if (parentIssue.getIssueType().getName() != 'Task')
{return}

if (parentIssue.summary.contains("Markup Master Task"))
{
numMarkups = Integer.parseInt(parentIssue.summary);
}

if (numMarkups >= 50)
{
return
}
else
{
def i = 0
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()

for (i; i < numMarkups; i++)
{
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setSummary("Markup" + i.toString())
newSubTask.assignee == parentIssue.assignee
newSubTask.components == parentIssue.getComponents()
newSubTask.setParentObject(parentIssue)

newSubTask.setPriorityId(constantManager.getPriorities().find {
it.getName() == "P2" }.id)

newSubTask.setProjectObject(parentIssue.getProjectObject())

newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task" }.id) }

}


}
}

 

1 answer

1 vote
Jenna Davis
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.
December 19, 2017

Hello, 

There's a few other questions up that already cover similar scripts to this:

https://community.atlassian.com/t5/Answers-Developer-Questions/Hi-Can-any-one-guide-me-how-to-create-Subtasks-in-post-function/qaq-p/493429

https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-auto-create-multiple-subtasks-in-JIRA-by-groovy/qaq-p/559686

Let me know if you still need help getting something to work after reading over the answers there. :)

Jenna

Jordan Klein December 19, 2017

Thank you @Jenna Davis! I've referenced both of the answers listed.

Both of the threads use this snippet at the bottom of their scripts, but it's throwing an error for me. Particularly, it's saying the word String is an "unexpected token" Any idea what's causing that? 

 

 Map&lt;String,Object&gt; newIssueParams = ["issue" : newSubTask] as Map&lt;String,Object&gt;
    issueManager.createIssueObject(user.directoryUser, newIssueParams)
    subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser)
    log.info "Issue with summary ${newSubTask.summary} and assignee ${assigneeId} created"
}

  

Mateusz Muller March 30, 2018

I was running into the same error, as i'm not a developer i had to look it up and I found the solution. Code is messed up by the website, this is how the line should look like:

 Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>

Aidan Derossett _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.
April 5, 2018

Yeah, community likes to finagle the code a bit. The "&lt" and "&gt" are the html encodings for "<" (less-than) and ">" (greater-than).

Nice fix!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events