Defining a Field-Level Validation Rule
A field-level validation rule is a constraint you can define on any standard or custom field. It is evaluated whenever the corresponding field's value is set.
When the rule executes, the field's value has not been assigned yet and your rule acts as
a gatekeeper to its successful assignment. The expression (or longer script) you write
must return a boolean
value that indicates whether the value is valid.
If the rule returns true
, then the field assignment will succeed so
long as all other field-level rules on the same field also return true
.
If the rule returns false
, then this prevents the field assignment from
occurring, the invalid field is visually highlighted in the UI, and the configured error
message is displayed to the end user. Since the assignment fails in this situation, the
field retains its current value (possibly null
, if the value was
null
before), however the UI component in the web page allows the
user to see and correct their invalid entry to try again. Your script can use the
newValue
keyword to reference the new value that will be assigned
if validation passes. To reference the existing field value, use the
oldValue
keyword. A field-level rule is appropriate when the rule
to enforce only depends on the new value being set.
For example, consider a TroubleTicket
object with a
Priority
field. To validate that the number entered is between 1
and 5, your field-level validation rule would look like this:
-
Field Name: Priority
-
Rule Name:
Validate_Priority_Range
-
Error Message:
The priority must be in the range from 1 to 5
Rule Body
newValue == null || (1..5).contains(newValue as Integer)
A
depends on the values of one
or more other fields (e.g.
Y
and
Z
),
then create an object-level rule and programmatically signal which field or fields
should be highlighted as invalid to the user as explained in Setting Invalid Fields for the UI in an Object-Level Validation Rule.