The following example modifies the checklist and shows multiple use cases, it does:
Uncheck all items
Remove the status from items with “In Progress” status
Make the item named “Run all tests” optional
...
A common use case is to modify existing items in a checklist to change some of their properties.
Info |
---|
See Checklist Classes and API to see all available checklist item methods. |
To modify the items you first need to retrieve the current list of items from the custom field.
Code Block | ||
---|---|---|
| ||
def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) event.issue.getCustomFieldValue(checklistCustomField); |
You then loop over the collection and apply the desired changes. For example, you can clear all the statuses from the items.
Code Block | ||
---|---|---|
| ||
// Remove statuses of the Checklist
for (ChecklistItem item : existingChecklistValue) {
item.setStatusId("none");
} |
Finally, you update the custom field passing it the same collection that was initially extracted from the custom field.
Code Block |
---|
checklistCustomFieldType.updateValue(checklistCustomField, event.issue, existingChecklistValue); |
...
Following is a complete example that can be used to modify a checklist by:
Unchecking all items.
Clearing items with “In Progress” statuses.
Making optional the item “Run all tests”.
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(); // 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); |