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

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 Community Champion Dec 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

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"
}

  

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>

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
Community showcase
Posted Thursday in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

1,177 views 4 5
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