Skip to main content
Skip table of contents

Load Templates based on a Select field

You can reuse existing lists of items for different issues by importing a template into a checklist with ScriptRunner. A template’s owner and/or project administrator(s) can edit a template without editing the associated script.

With ScriptRunner Behaviours, you can import different templates based on other field values such as select fields.


Steps

  1. Create a new ScriptRunner Behaviour and select the desired project and issue type mappings.

  2. In the Add field section, add the Select field.

    image-20240517-135940.png

  3. Add a condition on your Select Field script so that it only runs on the Create action.

    image-20240517-154654.png

  4. Select “Create script” to add a script when your Select field changes.

    image-20240517-154951.png

  5. In the script, use the following snippets:

    1. Create a method which can import a template inside the checklist:

      GROOVY
      public importTemplateInChecklist(String checklistFieldName, int templateId) {
          // Retrieve the Custom Field, Field Type and Templates Manager
          def checklist = getFieldByName(checklistFieldName);
          def customFieldManager = ComponentAccessor.getCustomFieldManager();
          def checklistCustomField = customFieldManager.getCustomFieldObject(checklist.getFieldId());
          def checklistCustomFieldType = (ChecklistCFType) checklistCustomField.getCustomFieldType();
          TemplatesManager templatesManager = checklistCustomFieldType.templatesManager;
      
          // Import the template
          Template template = templatesManager.getTemplate(templateId);
          checklist.setFormValue(template.getItems().toJson());
      }
    2. Retrieve the select field’s value and conditionally import a template based on its value.

      GROOVY
      def selectFieldName = "My Select Field";
      def checklistFieldName = "DoD";
      
      def selectValue = getFieldByName(selectFieldName).getValue();
      if (selectValue == "Choice 1") {
          int templateId = 1;
          importTemplateInChecklist(checklistFieldName, templateId);
      } else if (selectValue == "Choice 2") {
          int templateId = 2;
          importTemplateInChecklist(checklistFieldName, templateId);
      }

Full example

Here is a full example where we import templates based on two different choices in a Select field.

GROOVY
import com.atlassian.jira.component.ComponentAccessor;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.okapya.jira.customfields.configuration.*;

public importTemplateInChecklist(String checklistFieldName, int templateId) {
    // Retrieve the Custom Field, Field Type and Templates Manager
    def checklist = getFieldByName(checklistFieldName);
    def customFieldManager = ComponentAccessor.getCustomFieldManager();
    def checklistCustomField = customFieldManager.getCustomFieldObject(checklist.getFieldId());
    def checklistCustomFieldType = (ChecklistCFType) checklistCustomField.getCustomFieldType();
    TemplatesManager templatesManager = checklistCustomFieldType.templatesManager;

    // Import the template
    Template template = templatesManager.getTemplate(templateId);
    checklist.setFormValue(template.getItems().toJson());
}

def selectFieldName = "My Select Field";
def checklistFieldName = "DoD";

def selectValue = getFieldByName(selectFieldName).getValue();
if (selectValue == "Choice 1") {
    int templateId = 1;
    importTemplateInChecklist(checklistFieldName, templateId);
} else if (selectValue == "Choice 2") {
    int templateId = 2;
    importTemplateInChecklist(checklistFieldName, templateId);
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.