Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The following example removes all items with the “N/A” statusYou might want to remove some items from the checklist, but not all. This can be useful to filter out undesired items in the checklist.

Info

If you want to remove all items or reset the checklist, see Update the checklist with a new set of items instead.

...

To do so, retrieve the checklist from the issue and then remove items from the ArrayList:

Note

Notice the .toList() in the loop, which avoids concurrency exceptions (deleting an item while lopping on it). An iterator can also be used.

Code Block
languagejava
// Filter items, only "Only Accepted Item Name" can be part of the Checklist
def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) issue.getCustomFieldValue(checklistCustomField);
for (ChecklistItem item : existingChecklistValue.toList()) {
    if (item.getName() != "Only Accepted Item Name") {
        existingChecklistValue.remove(item);
    }
}

// Update the issue with the filtered checklist
checklistCustomFieldType.updateValue(checklistCustomField, issue, existingChecklistValue);

...

The following example removes all items which has the “Not Applicable” status:

Code Block
languagejava
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();

// Remove N/A items from the Checklist
def issue = (MutableIssue) event.issue;
def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) issue.getCustomFieldValue(checklistCustomField);
for (ChecklistItem item : existingChecklistValue.toList()) {
    // Remove all items with "Not Applicable" status 
    if (item.getStatusId() == "notApplicable") {
        existingChecklistValue.remove(item);
    }
}

// Update the issue with the checklist
checklistCustomFieldType.updateValue(checklistCustomField, issue, existingChecklistValue);