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
Create a new ScriptRunner Behaviour and select the desired project and issue type mappings.
In the Add field section, add the Select field.
Add a condition on your Select Field script so that it only runs on the Create action.
Select “Create script” to add a script when your Select field changes.
In the script, use the following snippets:
Create a method which can import a template inside the checklist:
GROOVYpublic 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()); }
Retrieve the select field’s value and conditionally import a template based on its value.
GROOVYdef 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.
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);
}