Versions Compared

Key

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

You may want to remove some items from a checklist, but not all of them. This can be useful to filter out unnecessary items in a checklist.

Info

To remove all items or reset a checklist, see /wiki/spaces/CHKDOC5/pages/3222536217 Replace all items in a checklist using ScriptRunner instead.


Steps

  1. Get the custom field, custom field type and items of your Checklist field.

    Code Block
    languagegroovy
    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. Loop through your items and remove the items you wanted removed.

    Code Block
    languagegroovy
    // The "toList()" here is important. If not specified, the script will fail with a ConcurrentModificationException
    for (ChecklistItem item : items.toList()) {
        if (item.getName() != "Only Accepted Item Name") {
            items.remove(item);
        }
    }

  3. Update the issue with the updated item list.

    Code Block
    languagegroovy
    checklistCFType.updateValue(checklistField, issue, items);

Full example

Here is a full example where items are removed from a Checklist field.

Code Block
languagegroovy
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.atlassian.jira.component.ComponentAccessor

// In this example the issue object comes from an event like in ScriptRunner's Custom Listeners
def issue = event.issue;

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);

// The "toList()" here is important. If not specified, the script will fail with a ConcurrentModificationException
for (ChecklistItem item : items.toList()) {
    if (item.getName() != "Only Accepted Item Name") {
        items.remove(item);
    }
}

// Update the value in the issue with the updated collection
checklistCFType.updateValue(checklistField, issue, items);

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