Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Create a Sub-Task When the Parent Issue Has a Specific Label

Features
Listeners
Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.14)
compatibility bullet
ScriptRunner For Jira (6.18.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParametersImpl

final newLabel = 'create_subtask'

def issue = event.issue
if (!issue.subTask && newLabel in issue.labels*.label) {
    createSubtask(issue)
}

def createSubtask(Issue parentIssue) {
    def subTaskManager = ComponentAccessor.subTaskManager
    def asUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
    def constantsManager = ComponentAccessor.constantsManager
    def issueService = ComponentAccessor.issueService

    def subtaskIssueType = constantsManager.allIssueTypeObjects.findByName('Sub-task')

    assert subtaskIssueType?.subTask

    // Fill the required fields
    def issueInputParameters = new IssueInputParametersImpl()
    issueInputParameters
        .setProjectId(parentIssue.projectId)
        .setIssueTypeId(subtaskIssueType.id)
        .setSummary('A new subtask')
        .setDescription('A description')
        .setReporterId(asUser.username)

    def createValidationResult = ComponentAccessor.issueService.validateSubTaskCreate(asUser, parentIssue.id, issueInputParameters)
    if (!createValidationResult.valid) {
        log.error createValidationResult.errorCollection
        return
    }

    def newIssue = issueService.create(asUser, createValidationResult).issue
    subTaskManager.createSubTaskIssueLink(parentIssue, newIssue, asUser)
}

Having an issue with this script?
Report it here