Skip to main content
Skip table of contents

Replace all items in a checklist using ScriptRunner

A common use case involves clearing the checklist’s existing content and rebuilding it from the ground up.

You can also use this approach to simply clear the checklist.


Steps

  1. Get the custom field and custom field type 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();

  2. Create an empty ChecklistItem list. This is what will be used as the new item collection for your checklist.

    GROOVY
    // Starting from an empty list to replace the current items.
    Collection<ChecklistItem> items = new ArrayList<ChecklistItem>();

  3. 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.
    If you want to simply clear the checklist, you can skip this step!

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

  4. Update the custom field with the new collection.

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

Full example

Here is a full example where we replace the checklist’s current items with new ones.

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

// Starting from an empty list to replace the current items.
Collection<ChecklistItem> items = new ArrayList<ChecklistItem>();

// In this example we created a small function to help create items without copying code.
// 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.