A common use-case is to clear the checklist’s current content and build it from the ground up
First, you will need to create an empty collection (ArrayList<ChecklistItem
) that will contain the new items.
def ArrayList<ChecklistItem> newChecklistValue = new ArrayList<ChecklistItem>();
You then create an item and add it to the collection.
def item = checklistCustomFieldType.getSingularObjectFromString('{"name": "item name"}'); newChecklistValue.add(item);
Finally, you update the custom field passing it the newly created collection.
checklistCustomFieldType.updateValue(checklistCustomField, event.issue, newChecklistValue);
Following is a complete example that can be used to update a checklist with a new set of items:
import com.onresolve.scriptrunner.runner.customisers.WithPlugin; import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.MutableIssue; @WithPlugin("com.okapya.jira.checklist") import com.okapya.jira.customfields.*; def ChecklistItem createChecklistItem(ChecklistCFType cfType, String name, boolean checked, String status, boolean mandatory, int rank) { def itemJson = """ { "name": "${name}", "checked": ${checked ? "true" : "false"}, "statusId": "${status == null || status == "" ? "none" : status}", "mandatory": ${mandatory ? "true" : "false"}, "rank": ${rank} } """; return cfType.getSingularObjectFromString(itemJson); } // Retrieve the Custom Field Type for the Checklist Custom Field def customFieldManager = ComponentAccessor.getCustomFieldManager(); def checklistCustomField = customFieldManager.getCustomFieldObject("customfield_10013"); def checklistCustomFieldType = (ChecklistCFType) checklistCustomField.getCustomFieldType(); // Create a new Checklist def ArrayList<ChecklistItem> newChecklistValue = new ArrayList<ChecklistItem>(); newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Compile the solution", false, null, true, 0) ); newChecklistValue.add( createChecklistItem(checklistCustomFieldType, "Run all tests", true, "notApplicable", false, 1) ); // Update the issue with the new checklist def issue = (MutableIssue) event.issue; checklistCustomFieldType.updateValue(checklistCustomField, issue, newChecklistValue);