Create Subtasks Based on Form Field Values

Purpose

Hosting

Compatible Tools

Purpose

Hosting

Compatible Tools

Create subtasks based on the values of a choice question (checkbox, dropdown or multi-select dropdown) in a form.

Server or Data Center

Any scripting tool, such as JMWE, Power Scripts or Scriptrunner, that uses Groovy.

Script

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.bc.issue.properties.IssuePropertyService import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.user.ApplicationUser import com.atlassian.jira.entity.property.EntityPropertyService import org.apache.commons.lang3.StringUtils ////If used in a transition, comment next line Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ISSUE-1") String formName = "Form Name" String fieldName = "Field Name" ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser() IssuePropertyService issuePropertyService = ComponentAccessor.getComponentOfType(IssuePropertyService.class) IssueManager issueManager = ComponentAccessor.getIssueManager() def issueService = ComponentAccessor.issueService def constantsManager = ComponentAccessor.constantsManager Closure createSubtask = { Issue parentIssue, String summary, String priorityName, String subtaskType -> def subtaskIssueTypes = constantsManager.allIssueTypeObjects.findAll { it.subTask } def subTaskIssueType = subtaskIssueTypes.findByName(subtaskType) def priority = constantsManager.priorities.findByName(priorityName) def issueInputParameters = issueService.newIssueInputParameters().with { setProjectId(parentIssue.projectObject.id) setIssueTypeId(subTaskIssueType.id) setReporterId(loggedInUser.getUsername()) setSummary(summary) setPriorityId(priority.id) } def validationResult = issueService.validateSubTaskCreate(loggedInUser, parentIssue.id, issueInputParameters) def issueResult = issueService.create(loggedInUser, validationResult) def subtask = issueResult.issue ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, subtask, loggedInUser) } Closure<Long> getFormId = { def property, String name -> def slurper = new groovy.json.JsonSlurper() def parsedJson = slurper.parseText(property.value) String id parsedJson.forms.each(){ form -> if(form.name.equals(name)){ id = form.id.toString() return } } if(id){ return Long.valueOf(id) } } Closure<String> getFieldId = { def property, String field -> def slurper = new groovy.json.JsonSlurper() def parsedJson = slurper.parseText(property.value) for (def question : parsedJson.design.questions){ if(field.equals(question.value.label)){ return question.key } } } Closure getCheckboxFieldValues = { def property, String field -> def slurper = new groovy.json.JsonSlurper() def parsedJson = slurper.parseText(property.value) for (def answer : parsedJson.state.answers){ String fieldId = getFieldId(property, field) if(fieldId.equals(answer.key)){ return answer.value.choices } } } Closure getCheckboxFieldInfo = { def property, String field -> def slurper = new groovy.json.JsonSlurper() def parsedJson = slurper.parseText(property.value) for (def question : parsedJson.design.questions){ if(field.equals(question.value.label)){ return question.value.choices } } } def allProperties = issuePropertyService.getProperties(loggedInUser, issue.id) def formId for(def property : allProperties){ if(property.key.equals("proforma.forms")){ formId = getFormId(property, formName) } } for(def property : allProperties){ if(property.key.equals("proforma.forms" + ".i" + formId) && property.value.contains("\"schemaVersion\":8")){ def choiceValues = getCheckboxFieldValues(property, fieldName) def fieldInfo = getCheckboxFieldInfo(property, fieldName) for (def info : fieldInfo){ if(info.label.equals("Create First Subtask") && choiceValues.contains(info.id)){ createSubtask(issue, "First Sub-task", "Medium", "Sub-task") } else if(info.label.equals("Create Second Subtask") && choiceValues.contains(info.id)){ createSubtask(issue, "Second Sub-task", "Medium", "Sub-task") } } } }

Installation instructions

  • If used in a transition:

    • Comment line 12

    • Change formName in line 13 to the name of the form that has the choice field used in the script.

    • Change fieldName in line 14 to the name/label of the choice field that has the options used for different sub-task creation.

    • Change value inside option.contains() in lines 101 and 103 to the choice values. Values must be between quotation marks.

    • Change parameters inside createSubtask() in lines 102 and 104 to:

      • Issue summary (in quotation marks)

      • Issue priority (must match priority name and be in quotation marks)

      • Sub-task Issue Type (must match a sub-task issue type name and be in quotation marks)

    • More else if clauses can be added if more options exist in the field. The pattern must be the same as lines 103 to 105.

  • If used in the script console:

    • Change issue key in line 12 to the issue that has the form with the field used in the script.

    • Change formName in line 13 to the name of the form that has the choice field used in the script.

    • Change fieldName in line 14 to the name/label of the field that has the choice options used for different sub-task creation.

    • Change value inside option.contains() in lines 101 and 103 to the values in the boxes of the field used in the script. Values must be in quotation marks.

    • Change parameters inside createSubtask() in lines 102 and 104 to:

      • Issue summary (in quotation marks)

      • Issue priority (must match priority name and be in quotation marks)

      • Sub-task Issue Type (must match a sub-task issue type name and be in quotation marks)

    • More else if clauses can be added if more options exist in the field. The pattern must be the same as lines 103 to 105.

Possible use cases

Use this script to create subtasks based on choice options selected in a form. For example, create subtasks for IT and Facilities based on checkboxes on an employee onboarding form.

Limitations

  • All options must be on the same choice field inside the same form.

  • The code between lines 101 and 105 may need to be amended to include other fields that have been set as required for creating subtasks in your instance.

  • If there are multiple copies of the same form on the issue, the script will run on the last (most recently added) copy of the form.

  • Issues can only be created for Subtask (child) issue types (as opposed to standard issue types).

  • You will need to refresh in order to see the newly added subtasks.

  • This script may not work if the form is over 32 KB. You can limit the size of your forms by using multiple smaller forms, and by including character/word limits on your text fields.

  • This script will not work on Legacy Forms.