/
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

  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.

    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.

    // 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.

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

     

  5. 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.

Related content