/
Running custom validations using ScriptRunner
Running custom validations using ScriptRunner
Although Checklist for Jira already offers a highly versatile validator, you may sometimes need a custom-scripted validator for more complex validations.
This guide will show you how to build a simple validator, which you can then tailor to your needs.
Steps
Retrieve the checklist items from the issue, as seen in the Get a checklist field’s items in an issue example.
Keep track of your validation as you loop through the items.
def valid = true; // In this example, we want all items to have a status. // The moment we find an item without a status we want the validation to fail. for (ChecklistItem item : items) { if (!item.hasStatus() && !item.getIsHeader()) { valid = false; // The break instruction stops the loop from continuing. // This isn't mandatory but will help with optimization. break; } }
Throw an exception if the validation fails.
if (!valid) { // Jira requires an InvalidInputException throw new InvalidInputException("All items must have statuses"); }
Full example
Here is a full example of a custom validator that verifies that all items have a status.
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
@WithPlugin("com.okapya.jira.checklist")
import com.okapya.jira.customfields.*;
import com.atlassian.jira.component.ComponentAccessor;
import com.opensymphony.workflow.InvalidInputException;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
// Replace 10000 with your Checklist field's ID
def checklistField = customFieldManager.getCustomFieldObject("customfield_10000");
def ArrayList<ChecklistItem> items = (ArrayList<ChecklistItem>) issue.getCustomFieldValue(checklistField);
def valid = true;
for (ChecklistItem item : items) {
if (!item.hasStatus() && !item.getIsHeader()) {
valid = false;
break;
}
}
if (!valid) {
throw new InvalidInputException("At least one item must have a status in the '" + checklistField.getName() + "' Checklist.");
}
SERVER documentation (On Cloud? Go here.)
Have questions? Contact our Service Desk for help anytime.
, multiple selections available,
Related content
Updating items in checklists using ScriptRunner
Updating items in checklists using ScriptRunner
Read with this
Adding items to checklists using ScriptRunner
Adding items to checklists using ScriptRunner
Read with this
Replace all items in a checklist using ScriptRunner
Replace all items in a checklist using ScriptRunner
Read with this
Importing a template using ScriptRunner
Importing a template using ScriptRunner
Read with this
Setting up a Modify Checklists post function
Setting up a Modify Checklists post function
Read with this
Setting up a Checklist Workflow Condition
Setting up a Checklist Workflow Condition
Read with this