Sub topic 2
Anatomy of a validation rule
This chapter assumes you can open Setup and the Object Manager, and that you know objects, fields, and records. It covers the parts of a rule and how they fit together. Later chapters cover the formula language in depth.
The four parts of a rule
Every validation rule on an object has the same pieces. You create it in Object Manager, under the object, in Validation Rules.
| Part | What it does | Notes |
|---|---|---|
| Rule Name | Identifies the rule in Setup and in deployments | Letters, numbers, and underscores only. It must be unique on the object. |
| Active | Turns enforcement on or off | An inactive rule is saved but never runs. |
| Error Condition Formula | Decides whether the data is invalid | Returns true for invalid data. |
| Error Message and Location | Tells the user what went wrong and where | The location is a field or the top of the page. |
A Description field is also available. Use it to record the business reason for the rule, because the formula alone rarely explains why the rule exists.
How the parts work together
When a user saves a record, Salesforce evaluates the formula of each active rule. If the formula returns true, the save stops and the error message appears. If it returns false, the rule has no effect.
The direction of the logic is the most common source of mistakes. The formula describes the bad state, not the good one.
A worked example
Suppose a deal in the Negotiation/Review stage must have a positive amount. Write the condition that describes the invalid data:
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)Read it aloud as a sentence: the stage is Negotiation/Review, and the amount is blank or not positive. When that sentence is true, the data is invalid.
Then fill in the remaining settings:
- Rule Name:
Amount_Required_In_Negotiation. - Active: checked.
- Error Message:
Enter an Amount greater than 0 before moving to Negotiation/Review. - Error Location: the Amount field.
Choosing the error location
The location controls where the user sees the message.
- Field: the message appears beside the chosen field and highlights it. Use this when one field is the problem, as in the example above.
- Top of Page: the message appears in a banner. Use this when the problem involves several fields or no single field.
The field you choose must appear on the page layout. If it doesn't, the user sees no message next to the field, so a rule that depends on a hidden field is hard to use. Choose a field that users can see.
Common errors
- Inverted logic. Writing the formula for valid data blocks every good record. Always describe the invalid state.
- Vague messages. "Invalid data" does not tell the user what to fix. Name the field and the expected value.
- Missing description. Another administrator cannot tell why the rule exists, so it may be deleted or loosened by accident.
Exercise
A rule's formula is AND(ISBLANK(Phone), ISPICKVAL(Type, "Customer")). When does it block a save, and which error location suits it best?
Show answer
It blocks the save when Type is Customer and Phone is blank, because the formula returns true for that invalid state. The error location should be the Phone field, since that is the field the user needs to fill in.