The following example removes all items with the “N/A” status:You might want to remove some items from the checklist, but not all. This can be useful to filter out unnecessary items in a checklist.
Info |
---|
If you want to remove all items or reset a checklist, see Update a checklist with a new set of items instead. |
You start by retrieving the current list of Checklist Items.
Code Block |
---|
|
def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) issue.getCustomFieldValue(checklistCustomField); |
You then remove the targeted items from the collection.
Note |
---|
Notice the use of the .toList() in the loop, which avoids method to avoid concurrency exceptions (.i.e deleting an item while lopping on it)iterating over the collection. An iterator can also be used in place. |
Code Block |
---|
|
for (ChecklistItem item : existingChecklistValue.toList()) {
if (item.getName() != "Only Accepted Item Name") {
existingChecklistValue.remove(item);
}
} |
Finally, you update the custom field passing it the newly created collection.
Code Block |
---|
checklistCustomFieldType.updateValue(checklistCustomField, issue, existingChecklistValue); |
...
Following is a complete example that can be used to removes all items which have the “Not Applicable” status:
Code Block |
---|
|
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); |