Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Script

Code Block
//// 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

  1. If used in a transition:

    • Comment line 2

    • Uncomment line 4

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

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

    • Change value inside option.label.equals() in lines 109 and 111 to the values in the boxes of the field used in the script. Values must be between quotation marks.

    • Change parameters inside createSubtask() in lines 110 and 112 to:

      • Issue summary (between quotation marks)

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

    • More else if clauses can be added if more options exist in the field, patterns must be the same as lines 111 to 113.

  2. If used in the script console:

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

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

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

    • Change value inside option.label.equals() in lines 109 and 111 to the values in the boxes of the field used in the script. Values must be between quotation marks.

    • Change parameters inside createSubtask() in lines 110 and 112 to:

      • Issue summary (between quotation marks)

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

    • More else if clauses can be added if more options exist in the field, patterns must be the same as lines 111 to 113

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

  • This script will not work in next-gen projects.

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

  • The code between lines 9 to 30 and 101 to 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 first 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.

...