Importing a template using ScriptRunner
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.
Steps
Find your template ID in the template list in Manage Apps.
Get the custom field and custom field type of your Checklist field.
GROOVYdef customFieldManager = ComponentAccessor.getCustomFieldManager(); // Replace the custom field ID with your custom field's ID def checklistField = customFieldManager.getCustomFieldObject("customfield_10101"); def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();
Get the template’s items with the ID from the first step.
GROOVY// Replace this ID with the template ID you want to use def templateId = 123 Template template = checklistCFType.templatesManager.getTemplate(templateId); Collection<ChecklistItem> templateItems = ChecklistSerializer.deserializeChecklist(template.getItemsJson());
Add the template’s items to the existing items in the issue.
GROOVYCollection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue); items.addAll(templateItems);
Update the issue with its new items.
GROOVYchecklistCFType.updateValue(checklistField, issue, items);
Full example
Here is a full example where a template is appended to an existing checklist.
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.atlassian.jira.component.ComponentAccessor;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.okapya.jira.customfields.configuration.*;
// In this example, the issue object comes from an event like in ScriptRunner's Custom Listeners
def issue = event.issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
// Replace the custom field ID with your custom field's ID
def checklistField = customFieldManager.getCustomFieldObject("customfield_10101");
def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();
// Replace this ID with the template ID you want to use
def templateId = 123;
Template template = checklistCFType.templatesManager.getTemplate(templateId);
Collection<ChecklistItem> templateItems = ChecklistSerializer.deserializeChecklist(template.getItemsJson());
Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue);
items.addAll(templateItems);
// Update the value in the issue with the new items
checklistCFType.updateValue(checklistField, issue, items);