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. their properties.
Info |
---|
See Checklist Classes and API to see all available |
...
checklist item 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: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 def ArrayList<ChecklistItem> existingChecklistValue = (ArrayList<ChecklistItem>) event.issue.getCustomFieldValue(checklistCustomField); for (ChecklistItem item : existingChecklistValue) { item.setStatusId("none"); } // Update the issue with the modified checklist |
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); |
The following example modifies a checklist with different changes depending on the items:
...
Here is a complete example that can be used to modify a checklist by:
Unchecking all items.
Remove the status from Clearing items with “In Progress” statusstatuses.
Modifies Making optional the item “Run all tests” to make it optional.
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); |