/
Importing a template using ScriptRunner
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.
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();
Get the template’s items with the ID from the first step.
// 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.
Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue); items.addAll(templateItems);
Update the issue with its new items.
checklistCFType.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);
SERVER documentation (On Cloud? Go here.)
Have questions? Contact our Service Desk for help anytime.
, multiple selections available,
Related content
Adding items to checklists using ScriptRunner
Adding items to checklists using ScriptRunner
Read with this
Updating items in checklists using ScriptRunner
Updating items in checklists using ScriptRunner
Read with this
Replace all items in a checklist using ScriptRunner
Replace all items in a checklist using ScriptRunner
Read with this
Converting Checkboxes fields to Checklist fields using ScriptRunner
Converting Checkboxes fields to Checklist fields using ScriptRunner
Read with this
Enhancing checklists using Groovy scripts
Enhancing checklists using Groovy scripts
Read with this
Developer guide
Developer guide
Read with this