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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

634 views 6 12
Read article

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