Skip to main content
Skip table of contents

Adding items to checklists using ScriptRunner

You may need to extend an existing checklist with additional items.


Steps

  1. Get the custom field, custom field type and current items of your Checklist field.

    GROOVY
    def customFieldManager = ComponentAccessor.getCustomFieldManager();
    // Remplace the custom field ID with whatever ID your custom field has
    def checklistField = customFieldManager.getCustomFieldObject("customfield_10101");
    def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();
    
    Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue);

  2. For each item you wish to add, you will need to create an item and append it to the retrieved collection. Make sure to set the rank of the item to the end of the checklist; if not, it may not end up where you expect.

    GROOVY
    // In this example we created a small function to help create items.
    // Feel free to adapt this function to better fit your use case.
    def addNewItem(String name, String statusId, Collection<ChecklistItem> currentItems) {
        ChecklistItem newItem = new ChecklistItem();
        newItem.setName(name);
        newItem.setStatusId(statusId);
        newItem.setRank(currentItems.size() + 1);
        currentItems.add(newItem);
    }
    
    // Add the items by calling the function we just created
    addNewItem("First new item", "inProgress", items);
    addNewItem("Second new item", "blocked", items);
    addNewItem("Third new item", "none", items);

  3. Update the custom field with the updated collection.

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

Full example

Here is a full example where we add a few items every time the script is run.

GROOVY
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.atlassian.jira.component.ComponentAccessor

// In this example the issue object comes from an event like in ScriptRunner's Custom Listeners
def issue = event.issue;

def customFieldManager = ComponentAccessor.getCustomFieldManager();
// Remplace the custom field ID with whatever ID your custom field has
def checklistField = customFieldManager.getCustomFieldObject("customfield_10101");
def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();

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

// In this example we created a small function to help create items.
// Feel free to adapt this function to better fit your use case.
def addNewItem(String name, String statusId, Collection<ChecklistItem> currentItems) {
    ChecklistItem newItem = new ChecklistItem();
    newItem.setName(name);
    newItem.setStatusId(statusId);
    newItem.setRank(currentItems.size() + 1);
    currentItems.add(newItem);
}

// Add the items by calling the function we just created
addNewItem("First new item", "inProgress", items);
addNewItem("Second new item", "blocked", items);
addNewItem("Third new item", "none", items);

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