Updating items in checklists using ScriptRunner
A popular use case is to change the checklist items depending on the value of another field. For example, you may want to check the value of a particular drop-down and then change the checklist accordingly.
Usually, this kind of manipulation is done using listeners on the Issue Create
or Issue Updated
events, but can be adapted to other events.
The checklist won’t automatically be updated in the Edit dialog or the issue view, as the user must commit their changes before the Groovy script can react to the event. Real-time updates would require JavaScript code on the webpage, and Checklist for Jira doesn’t currently offer a JavaScript API.
Steps
This example is built with a listener on the Issue Updated
event in mind. We will update the checklist’s items based on another field’s value (in this example customfield_10123
).
Get the other field’s value.
GROOVYdef customFieldManager = ComponentAccessor.getCustomFieldManager(); // In this example the issue comes from the listener's event. def issue = event.issue; // Change the field ID to whatever ID your field has def otherField = customFieldManager.getCustomFieldObject("customfield_10123"); // Our field returns a String value. You will probably need to adapt this to your use case. def otherFieldValue = (String) issue.getCustomFieldValue(otherField);
Add a guard clause that makes sure the other field has changed and that its value matches what you are looking for as a trigger.
GROOVYdef otherFieldHasChanged = event.getChangeLog().getRelated("ChildChangeItem").any { it.field == otherField.getName() }; if (!otherFieldHasChanged || otherFieldValue != "Should update checklist") { // Returning here will stop the script and won't update the checklist. return; }
Get the checklist field and its items.
GROOVY// Change the field ID to whatever ID your field has def checklistField = customFieldManager.getCustomFieldObject("customfield_10001"); def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType(); Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue);
Loop over the items and update the items you wish to update. In this example we want to mark all items with the
inProgress
status ID as completed by checking them and setting thedone
status ID.GROOVYfor (ChecklistItem item : items) { if (item.getStatusId() == "inProgress") { item.setStatusId("done"); item.setChecked(true); } }
Update the issue with the updated items.
GROOVYissue.setCustomFieldValue(checklistField, items); checklistCFType.updateValue(checklistField, issue, items);
Full example
Here is the full example of updating checklist items based on another field’s value.
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager();
// In this example the issue comes from the listener's event.
def issue = event.issue;
// Change the field ID to whatever ID your field has
def otherField = customFieldManager.getCustomFieldObject("customfield_10123");
// Our field returns a String value. You will probably need to adapt this to your use case.
def otherFieldValue = (String) issue.getCustomFieldValue(otherField);
def otherFieldHasChanged = event.getChangeLog().getRelated("ChildChangeItem").any { it.field == otherField.getName() };
if (!otherFieldHasChanged || otherFieldValue != "Should update checklist") {
// Returning here will stop the script and won't update the checklist.
return;
}
// Change the field ID to whatever ID your field has
def checklistField = customFieldManager.getCustomFieldObject("customfield_10001");
def checklistCFType = (ChecklistCFType) checklistField.getCustomFieldType();
Collection<ChecklistItem> items = (Collection<ChecklistItem>) checklistCFType.getValueFromIssue(checklistField, issue);
for (ChecklistItem item : items) {
if (item.getStatusId() == "inProgress") {
item.setStatusId("done");
item.setChecked(true);
}
}
issue.setCustomFieldValue(checklistField, items);
checklistCFType.updateValue(checklistField, issue, items);