Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

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()) {
          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()) {
      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.
  • No labels