Create Subtasks Based on Form Field Values
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 |
|
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 |
|