Create multiplite sub tickets

Hello,

Is it possible to auto create sub tasks, based on the count of values chosen in a multi selection custom field, so that in case a user has selected 3 values, 3 sub tickets should be created and each subtask should contain only one value chosen in the main ticket?

In Groups.jpg I have selected 3 groups, that means we should have 3 sub tickets after we create the main ticket, so that for the “Branch Jermuk” group option I should have one sub ticket, where the in the Group option only “Branch Jermuk” should be filled in and in the second sub ticket in the Group option only “Branch Kamar” option should be filled in, etc…

Can you please provide code for listener for this approach?

Thanks in advance

Hello,

I’ve developed and tested following script which is working for me. It Script-runner custom listener for issue created event for one single project:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.bc.issue.IssueService

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def subtaskManager = ComponentAccessor.getSubTaskManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def issueService = ComponentAccessor.getIssueService()


def createdIssue = issueManager.getIssueObject(event.issue.id)
def subTaskIssueTypeId = "10113"


if (createdIssue.getIssueTypeId() != subTaskIssueTypeId) {
	def multiGroupPickerCFId = 14602L
	def multiGroupPickerCF = customFieldManager.getCustomFieldObject(multiGroupPickerCFId)
	

    createdIssue.getCustomFieldValue(multiGroupPickerCF).each {
        it ->
            def issueInputParameters = new IssueInputParametersImpl()
            issueInputParameters.setReporterId(createdIssue.reporter.username)
            issueInputParameters.setProjectId(createdIssue.projectId)
            issueInputParameters.setIssueTypeId(subTaskIssueTypeId)
            issueInputParameters.setSummary(createdIssue.summary + " - " + it.name)
            issueInputParameters.addCustomFieldValue(multiGroupPickerCFId, it.name)

            IssueService.CreateValidationResult createValidationResult = issueService.validateSubTaskCreate(createdIssue.reporter, createdIssue.id, issueInputParameters);
            if (!createValidationResult.isValid()) {
                throw new IllegalStateException("Create subtask validation has failed due to: " + createValidationResult.getErrorCollection())
            }

            IssueService.IssueResult issueResult = issueService.create(createdIssue.reporter, createValidationResult)
            if (!issueResult.isValid()) {
                throw new IllegalStateException("Create subtask has failed due to: " + issueResult.getErrorCollection())
            }

            subtaskManager.createSubTaskIssueLink(createdIssue, issueResult.issue, createdIssue.reporter)
    }   
}

Listener relies on if (createdIssue.getIssueTypeId() != subTaskIssueTypeId) condition. There is a risk listener will be called for created sub-task as well which leads to cycle. It would be safer to rewrite this listener as a postfunction and put it on Create transition on desired issue.

I’ve implemented it for Group Multi Picker CF. I believe it shouldn’t be issue to rewrite it for different multi select CF (handle with options, not groups).

Test it properly before deploying on live environment.

Kind Regards
Josef