It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to Automatically create Sub - task using script runner post function?

We have a requirement to be able to automatically create a few sub tasks, when an issue type called Feature is created. I have tried to use the script runner in post function of Submit. I do not see any sub task created after the feature being created.

I tried with having no specific criteria , when creating a sub task other than the Target Type being sub task. Didn't work either. Am I missing some thing?

3 answers

Hi Srillatha

Below is a sample custom script that you can use in order to create a number of subtasks.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.manager.OptionsManager

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

Issue parentIssue = issue

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

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

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

// code in order to create the values to update a CascadingSelect custom field
def cascadingSelect = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("CascadingSelect")
def fieldConfig = cascadingSelect?.getRelevantConfig(parentIssue)
def listOfOptions = ComponentAccessor.getComponent(OptionsManager).getOptions(fieldConfig)
def parentOption = listOfOptions?.find {it.value == "AAA"}
def childOption = parentOption?.getChildOptions()?.find {it.value == "A2"}

def mapWithValues = [:]
mapWithValues.put(null, parentOption)
mapWithValues.put('1', childOption)

summariesList.each {
MutableIssue newSubTask = issueFactory.getIssue()
it.getName() == "Sub-task"
// Add any other fields you want for the newly created sub task
newSubTask.setCustomFieldValue(cascadingSelect, mapWithValues)

def 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) "Issue with summary ${newSubTask.summary} created"

Note. Add it as a post function -> script post function -> custom script post function and make sure this goes after the 'Creates the issue originally' action (because before that the issue doesn't really exist, unless you get it's values using the transientVars). Hope that helps.

PS. If you are in a JIRA v7.* then you will need the application user therefore use user instead of user.directoryUser

Hi Thanos,


Any chance you could help me with this line:

Map&lt;String,Object&gt; newIssueParams = ["issue" : newSubTask] as Map&lt;String,Object&gt

It seems like it has been HTML formatted.

Hey Shaw, I edited the snippet, now it should display the code fine. 

@Thanos Batagiannis -Adaptavist- How to add cascading field value while creating a subtask? 

// Add any other fields you want for the newly created sub task

Hey there, 

I updated the script above in order to include setting values to a cascading select list.

Thank you for script update. I have this error method and also am trying to add few more fields when subtask is created like

1. cascading select field

2. due date(issue due date)

3. dropdown single select

4.drop down single selectUntitled.png

Like Marc Moroz likes this

Hi @Thanos Batagiannis -Adaptavist- , @John River ,

How can I copy the fix version from the issue to the doc sub task I will create ?

Getting the same error

What troubleshooting have you done so far? Did you follow any document to create your script? Try following the instructions here in the JIRA ScriptRunner Built-in scripts.


Did you remember to publish the workflow?

If you want complete control you can use a script like the one @Thanos Batagiannis [Adaptavist] published above, although you should try to work out what the problem with the built-in script is first.

@Thanos Batagiannis -Adaptavist-  Can you help in modifying on of your script to Create Sub-Tasks based on Custom field value (numeric Value).

I want to create number of sub-tasks based on the field value of the custom field which would be filled at the time of ticket creation.


Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

269 views 1 2
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you