Create Subtasks Based on a Form Field Value.
Purpose | Hosting | Compatible Tools |
---|---|---|
Create subtasks based on the values of a choice question (checkbox, dropdown or multi-select dropdown) in a form. | Cloud | Any scripting tool, such as JMWE, Power Scripts or Scriptrunner, that uses Groovy. |
Script | //// When using this script in the script console uncomment the next line:
def issueKey = 'ISSUE-1'
//// When using this script in a transition uncomment the next line:
// def issueKey = issue.key
String formName = "Form Name"
String fieldName = "Checkbox Form Field"
Closure createSubtask = { String summary, String issueType ->
def issueResp = get("/rest/api/2/issue/${issueKey}")
.asObject(Map)
def parentIssue = issueResp.body as Map
def typeResp = get('/rest/api/2/issuetype')
.asObject(List)
def issueTypes = typeResp.body as List<Map>
def issueTypeId = issueTypes.find { it.subtask && it.name == issueType }?.id
def createDoc = [
fields: [
project: (parentIssue.fields as Map).project,
issuetype: [
id: issueTypeId
],
parent: [
id: parentIssue.id
],
summary: summary
]
]
def resp = post("/rest/api/2/issue")
.header("Content-Type", "application/json")
.body(createDoc)
.asObject(Map)
}
Closure getFormId = { def allProperties, String name ->
for(def formProperty : allProperties){
if(formProperty.key.equals("proforma.forms")){
for(def p : formProperty.value.forms) {
if(p.name.equals(name)){
return p.id
}
}
}
}
}
Closure<String> getFieldId = { def property, String name ->
for (def question : property.value.design.questions){
if(name.equals(question.value.label)){
return question.key
}
}
}
Closure getCheckboxFieldValue = { def property, String name ->
def fieldId = getFieldId(property, name)
for (def answer : property.value.state.answers){
if(answer.key.equals(fieldId)){
return answer.value.choices
}
}
}
Closure getCheckboxFieldInfo = { def property, String name ->
def fieldId = getFieldId(property, name)
for (def question : property.value.design.questions){
if(question.key.equals(fieldId)){
return question.value.choices
}
}
}
def proformaPropertyKeys = []
def allIssueProperties = get('/rest/api/3/issue/' + issueKey + '/properties/')
.header('Content-Type', 'application/json')
.asObject(Map)
if (allIssueProperties.status == 200){
for(def property : allIssueProperties.body.keys){
if(property.key.contains("proforma.forms")){
proformaPropertyKeys.add(property.key)
}
}
}
def formProperties = []
if(!proformaPropertyKeys.isEmpty()){
for(def key : proformaPropertyKeys){
def property = get('/rest/api/3/issue/' + issueKey + '/properties/' + key)
.header('Content-Type', 'application/json')
.asObject(Map)
if(property.status == 200){
formProperties.add(property.body)
}
}
}
def formId
if(!formProperties.isEmpty()){
formId = getFormId(formProperties, formName)
for(def formProperty : formProperties){
if(formProperty.key.equals("proforma.forms" + ".i" + formId) && formProperty.value.schemaVersion.equals(8)){
def fieldChoices = getCheckboxFieldValue(formProperty, fieldName)
def fieldInfo = getCheckboxFieldInfo(formProperty, fieldName)
for(def option : fieldInfo){
if(option.label.equals("Option 1") && fieldChoices.contains(option.id)){
createSubtask("Summary for Option 1", "Sub-task")
} else if(option.label.equals("Option 2") && fieldChoices.contains(option.id)){
createSubtask("Summary for Option 2", "Sub-task")
}
}
}
}
} |
Installation instructions |
|
Possible use cases | Use this script to create subtasks based on checkboxes on a form. For example, create subtasks for IT and Facilities based on check boxes on an employee onboarding form. |
Limitations |
|