Using scriptrunner I need to know how to create multiple subtasks as a postfunction

Hi,

I need to know how to create muliplte subtasks as a postfunction through a workflow transition.

I have to create upwards of 20+ subtasks per workflow status and need to know how they can be automatically added through transitions.

Does anyone have any script that will be of assistance.

1 answer

0 votes

Hi Rhian,

Do you need each of these sub-tasks to have different information? Or will all of the sub-tasks contain all of the same information? Creating sub-tasks automatically on a workflow function is already a feature built-in to ScriptRunner.

  1. Go to the workflow step you want to create sub-tasks on
  2. Click add Post Function
  3. Click Script Post Function
  4. Select "Create a sub-task"

Screen Shot 2017-08-09 at 8.29.08 AM.png

This built-in feature would work well if you need each sub-task to have different information, since you could customize each sub-task individually as a post-function. However, if you just need to create 20+ sub-tasks with the same information, you'd probably be better off with a post-function custom script like this:

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

def constantManager = ComponentAccessor.getConstantsManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()

Issue parentIssue = issue

if (parentIssue.getIssueTypeObject().getName() == 'Sub-task')
return

if (parentIssue.getIssueTypeObject().name != 'Feature')
return

def summariesList = ["Summary 1", "Summary 2", "Summary 3"]

summariesList.each {
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(parentIssue.assigneeId)
newSubTask.setSummary(it)
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)
// Add any other fields you want for the newly created sub task

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

//for JIRA v6.*
issueManager.createIssueObject(user.directoryUser, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser)
// for JIRA v7.*
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)

log.info "Issue with summary ${newSubTask.summary} created"
}

Thanks for replying Joshua,

 

I need to have different subtasks at different stages of the transition however there will be many subtasks and I have been told that a custom script will be best for this.

I have tried the custom script you have provided however I am still getting a failed response:

unable to resolve class for:

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

Struggling to get a working script at the moment haha.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

24,533 views 2 7
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