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

Srilatha Kolla February 3, 2016

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

5 votes
Thanos Batagiannis _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.
February 3, 2016

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')
return

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

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()
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
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)

log.info "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

John River October 19, 2017

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.

Thanos Batagiannis _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.
October 19, 2017

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

John River October 19, 2017

Thanks!

KRC February 12, 2018

@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
Thanos Batagiannis _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.
February 13, 2018

Hey there, 

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

KRC February 14, 2018

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
Dan27 November 19, 2018

Hi @Thanos Batagiannis _Adaptavist_ , @John River ,

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

Marc Moroz November 3, 2019

Getting the same error

0 votes
JamieA
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.
February 3, 2016

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.

Dan27 November 17, 2018

Cancel the reply.

Chander Korshika June 11, 2019

@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.

Thanks

Aarón López López March 16, 2020

Hi @Chander Korshika I am trying the same thing and I think I am missing something

Can you give me an example?

Thank you.

Chander Korshika May 20, 2020

@Aarón López López  I apologies for delayed reply.So my ask was : 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.

so at the time of submitting request type there is custom_field : order Type and dependent field is order quantity :

Order Type : A

Order Quantity : 2

so based on quantity i want to create 2 sub-tasks under the parent ticket automatically and parent should be linked to child and vice-versa.

Let me know if you have any other question.

Best Regards,
Chander Korshika

Hema June 15, 2021

Hi @Chander Korshika , 

Even I'm trying to get a similar scenario done, the only difference is that, I need tasks/stories to be created based on a custom field value selected.

Have you got any resolution on this? Thought this might help my scenario as well.

Note : Using script runner for JIRA cloud.

 

Thanks in advance.

Chander Korshika June 15, 2021
Hi @Hema ,
You can try below code snippet in post-functions but you may have to change the issue type from subtask to tasks/stories as per your need.

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().getLoggedInUser()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def breakfast = customFieldManager.getCustomFieldObject("customfield_1111")

def reporter = ComponentAccessor.jiraAuthenticationContext.loggedInUser;


Issue parentIssue = issue
//To test in Script console or for a specific issue uncomment below line
//Issue parentIssue = issueManager.getIssueObject("ABC-189")

def breakfastCount = parentIssue.getCustomFieldValue(breakfast)

for(int i=0 ; i < (int)Double.parseDouble(breakfastCount.toString()) ;i++)
{
def subtaskSummary = "Breakfast "+(i+1)

MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(parentIssue.assigneeId)
newSubTask.setReporter(reporter)
newSubTask.setSummary(subtaskSummary)
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{
it.getName() == "Sub-task"
}.id)


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

issueManager.createIssueObject(reporter, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, reporter)


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

Best Regards
Chander Korshika
Chander Korshika June 23, 2021

@Hema  Did it worked for you ? the above solution 

Hema June 28, 2021

Hi @Chander Korshika ,

Apologies for late reply. I'm trying the solution using REST APIs, as I'm using JIRA cloud.

0 votes
GabrielleJ
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.
February 3, 2016

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.

Reference: https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html

Suggest an answer

Log in or Sign up to answer