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
Get the custom field and custom field type of your Checklist field.
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();
Create an empty
ChecklistItem
list. This is what will be used as the new item collection for your checklist.// Starting from an empty list to replace the current items. Collection<ChecklistItem> items = new ArrayList<ChecklistItem>();
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!// 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 custom field with the new collection.
Full example
Here is a full example where we replace the checklist’s current items with new ones.