A common use case is to modify the existing items in a checklist, in order to change some of its properties like checked, status, mandatory. See Checklist Classes and API to see available update methods.
Modifying items is simple, retrieve the custom field value and then loop on it to apply desired changes. The following example remove statuses from a Checklist:
// Remove statuses of the Checklist def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) event.issue.getCustomFieldValue(checklistCustomField); for (ChecklistItem item : existingChecklistValue) { item.setStatusId("none"); } // Update the issue with the modified checklist checklistCustomFieldType.updateValue(checklistCustomField, event.issue, existingChecklistValue);
The following example modifies a checklist with different changes depending on the items:
Unchecks all items.
Remove the status from items with “In Progress” status.
Modifies the item “Run all tests” to make it optional.
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.*; // 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(); // Modify existing items in the Checklist def issue = (MutableIssue) event.issue; def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) issue.getCustomFieldValue(checklistCustomField); for (ChecklistItem item : existingChecklistValue) { // Uncheck all items item.setChecked(false); // Remove statuses for items "In progress" if (item.getStatusId() == "inProgress") { item.setStatusId("none"); } // Item "Run all tests" must be optional (not mandatory) if (item.getName() == "Run all tests") { item.setDiscretionary(true); } } // Update the issue with the checklist checklistCustomFieldType.updateValue(checklistCustomField, issue, existingChecklistValue);