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

  1. Retrieve the checklist items from the issue, as seen in the Get a checklist field’s items in an issue example.

  2. 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; } }

     

  3. 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.