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.

    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.

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

    checklistCFType.updateValue(checklistField, issue, items);

Full example

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


SERVER documentation (On Cloud? Go here.)
Have questions? Contact our Service Desk for help anytime.