Defining an Object-Level Validation Rule

An object-level validation rule is a constraint you can define on any business object. It is evaluated whenever the framework attempts to validate the object.

Use object-level rules to enforce conditions that depend on two or more fields in the object. This ensures that regardless of the order in which the user assigns the values, the rule will be consistently enforced. The expression (or longer script) you write must return a boolean value that indicates whether the object is valid. If the rule returns true, then the object validation will succeed so long as all other object-level rules on the same object return true. If the rule returns false, then this prevents the object from being saved, and the configured error message is displayed to the end user.

For example, consider a TroubleTicket object with Priority and DueDate fields. To validate that a trouble ticket of priority 1 or 2 cannot be saved without a due date, your object-level rule would look like this:

  • Rule Name: Validate_High_Priority_Ticket_Has_DueDate

  • Error Message: A trouble ticket of priority 1 or 2 must have a due date

Rule Body

// Rule depends on two fields, so must be written as object-level rule
if (Priority <= 2 && DueDate == null) {
  // Signal to highlight the DueDate field on the UI as being in error
  adf.error.addAttribute('DueDate')
  return false
}
return true