Skip to main content
Skip table of contents

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

  1. Find your template ID in the template list in Manage Apps.

  2. Get the custom field and custom field type of your Checklist field.

    GROOVY
    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();

  3. 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());

  4. Add the template’s items to the existing items in the issue.

    GROOVY
    Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue);
    items.addAll(templateItems);

  5. Update the issue with its new items.

    GROOVY
    checklistCFType.updateValue(checklistField, issue, items);

Full example

Here is a full example where a template is appended to an existing checklist.

GROOVY
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);
JavaScript errors detected

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

If this problem persists, please contact our support.