How to auto create multiple subtasks in JIRA by groovy

Hardik Parekh March 28, 2016

Hi,

How can I automatically creates few sub-tasks with fix summary I define for each subtasks on JIRA issue Creation  (Story / task any normal issue type) & also put quick check like if Template (Custom field - type select list single choice) = Template 1 then create sub-task else ignore.

Based on that I will put another post function with same script & check if Template = Template 2 then create sub task else ignore it.

I want to use groovy script for this as script runner is providing option to put inline groovy or file groovy.

Capture 2016-03-01 at 15.44.10.png

 

Currently I am able to create sub tasks based on Script runner post function "Create Sub task" but if I need to create 15 sub tasks then I need to add separate 15 PFs in workflow & if need to modify summary then again update 15 Post functions !!

Capture 2016-03-01 at 15.38.21.pngCapture 2016-03-01 at 15.39.55.png

 

So please suggest if you are using groovy for auto creating subtasks. I already searched old answers but as I am new to groovy I am not getting where to put name/id of my project & where to put name of my subtasks. also I need small check of custom field as I mentioned above. So please suggest on this.

 

Thanks,

Hardik

2 answers

0 votes
Maille Christine March 28, 2017

I have tested the script and it works.

How can I modify it to replace the current list for summariesList by the selected values from a field type Checkboxes

Thank you in advance for your help

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.
March 28, 2017
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def customFieldManager = ComponentAccessor.customFieldManager
def cf = customFieldManager.getCustomFieldObjectByName("My checkboxes field")
def summariesList = (issue.getCustomFieldValue(cf) as Collection<Option>)*.value
Like web likes this
Marc-Antoine Gosselin August 16, 2018

Hi Jamie,

 

This is awesome do you know if their is a way to have multiple field for one subtask.

 

For exemple.

I try to have multiple column to select from

C1 (Ressource) C2(Summary) C3(Estimate effort) C4 (Start Date) c5 (End Date)

The first one has multiple

C2 is a text box

C3 Is a number

C4-C5 are date picker

 

I want to be able to add mutliple line to create multiple subtaks....

Is this can be adapt to have this kind of setup

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.
March 29, 2016

You want something like the following. Just check custom field values to see whether to create the sub-tasks or not:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
def parentIssue = event.issue
if (parentIssue.getIssueType().getName() != 'New Employee')
    return
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def summariesList = ["summary1",
                     "summary2",
                     "summary3",
                     "summary4",
                     "summary5",
                     "summary6"]
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
summariesList.each { subTaskSummary ->
    MutableIssue newSubTask = issueFactory.getIssue()
    newSubTask.setSummary(subTaskSummary)
    newSubTask.setParentObject(parentIssue)
    newSubTask.setPriorityId(constantManager.getPriorities().find {
        it.getName() == "High"
    }.id)
    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
    log.debug("New issue ${newSubTask}")
    Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
    issueManager.createIssueObject(user, newIssueParams)
    subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)
    log.info "Issue with summary ${newSubTask.summary} created"
}
Jon October 16, 2018

Hi Jamie, 

 

I'm looking to do something similar, but instead of creating a set number of subtasks I want to create X based on the value of a numeric field on the parent issue. The subtasks would inherit values from the parent for most fields. What would the script look like for something like that? 

Like Girish Ramachandran likes this

Suggest an answer

Log in or Sign up to answer