Sub topic 2
Anatomy of a validation rule
This chapter assumes you know the basic Salesforce data model (objects, fields, records), can find an object in Setup and the Object Manager, and can read spreadsheet-style formulas. You do not need Apex here. The earlier chapter covered what validation rules do and when they fire. This one covers the parts of a single rule and how they work together.
The four parts that matter
A validation rule on an object is built from a small set of properties. Four of them decide how the rule behaves:
| Part | What it controls | Example |
|---|---|---|
| Error condition formula | The test. When it evaluates to true, the save is blocked. | `Amount <= 0` |
| Error message | The text the user sees when the formula is true. | "Enter an Amount greater than zero." |
| Error location | Where the message appears: top of the page, or next to one field. | Field: Amount |
| Active flag | Whether Salesforce evaluates the rule at all. | Checked |
Two more properties are for people, not for the platform: the rule name and the description. They do not change behavior, but they matter when someone else has to maintain the rule.
Rule name and description
The rule name is the API name. It must start with a letter and can contain only letters, numbers, and underscores. It cannot end with an underscore or contain two underscores in a row. Use a name that says what the rule enforces, such as Amount_Required_In_Negotiation. A name like Rule1 forces the next admin to open the formula to learn its purpose.
The description is free text, and it is optional. Use it to record why the rule exists, such as the business requirement or a ticket number. The formula says what the rule checks, and the description says why.
The error condition formula
The formula must return a Boolean, and the result is the reverse of what many new admins expect: true means the data is invalid and the save is blocked. You write the condition that describes the bad record, not the good one.
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)Read this as a sentence: "The record is invalid when the stage is Negotiation/Review and the Amount is blank or not positive." If the stage is Prospecting, the first AND argument is false, so the whole formula is false and the save goes through.
If a formula returns something other than a Boolean, such as text or a number, Salesforce rejects it when you save the rule. The Check Syntax button in the rule editor catches this and most typos before you save. It does not tell you whether the logic is right.
How the formula sees the record
The formula runs against the record as it will look after the save. That means:
- Fields the user edited have their new values.
- Fields the user did not touch keep their current values.
- On create, there is no earlier version of the record.
Functions such as ISCHANGED and PRIORVALUE, which compare new and old values, get their own chapter. For now, remember that the formula sees the pending values, not what is currently in the database.
The error message
The error message is the text a user sees when the formula is true. Write it as an instruction the user can act on. Compare these two messages for the Opportunity rule above:
- Weak: "Invalid Amount."
- Better: "Enter an Amount greater than zero before moving the Opportunity to Negotiation/Review."
The second message names the field, the problem, and the stage that triggers the rule. A later chapter covers message wording in more depth. Keep the length in mind, because the message field has a character limit (255 characters).
The error location
You choose where the message appears:
- Top of Page shows the message in a banner at the top of the record page or edit form. Use it when the rule involves several fields or when no single field is to blame.
- Field shows the message directly under one field you pick. Use it when one field clearly needs fixing.
Two practical points apply to the field option. The field you choose should appear on the page layout the user is working with. If it does not, the user may not see the message where you intended. Also, other save paths, such as the API, a flow, or Apex DML, have no page layout. They receive the message as an error on the record, and the error can name the field.
The active flag
The Active checkbox decides whether Salesforce evaluates the rule. An inactive rule stays in the org but never runs. This is useful in three situations:
- You are building a rule and want to save work in progress without affecting users.
- You need to stop a rule briefly, for example while a one-time data cleanup runs.
- You are retiring a rule and want to keep its definition for reference.
Deactivating a rule is easier to undo than deleting it, and it keeps the formula for review.
Existing records are not rechecked
A validation rule runs only when a record is saved. When you activate a rule, Salesforce does not scan existing data. Records that already break the rule stay as they are until someone edits them. At that point, the user must fix the violation to save, even if they changed an unrelated field. This surprises users. If you add a rule to an object with years of data, expect complaints from people editing old records, and consider running a report to find violations first.
The rule as metadata
When you deploy a rule between orgs or keep it in source control, it appears as a metadata file. Seeing it in that form shows how the four parts map to stored properties:
<?xml version="1.0" encoding="UTF-8"?>
<ValidationRule xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Amount_Required_In_Negotiation</fullName>
<active>true</active>
<description>Sales ops request: no late-stage deals without a positive Amount.</description>
<errorConditionFormula>AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)</errorConditionFormula>
<errorDisplayField>Amount</errorDisplayField>
<errorMessage>Enter an Amount greater than zero before moving the Opportunity to Negotiation/Review.</errorMessage>
</ValidationRule>Note the details:
fullNameis the rule name.errorDisplayFieldholds the field name for a field-level rule. For a top-of-page rule, this element is left out.- The
<=operator is written as<=because XML reserves the<character. The formula is the same once Salesforce reads it.
In a source-format project, this file lives under the object's validationRules folder, for example objects/Opportunity/validationRules/Amount_Required_In_Negotiation.validationRule-meta.xml.
Common mistakes in rule anatomy
- Writing the formula for valid data. If the rule blocks everything that should pass, the condition is probably inverted. Rephrase it as "the record is invalid when...".
- Putting a field-level message on a hidden field. The user sees nothing next to the field because it is not on their layout. Use top of page, or add the field to the layout.
- Vague messages. "Error" or "Invalid value" makes users file support requests. Name the field and the fix.
- Leaving rules active during data loads without a plan. A rule applies to the API as well as the UI. A later chapter covers bypass patterns for this.
- Missing descriptions. Six months later, nobody remembers why the rule exists, and nobody dares to remove it.
Exercise
A validation rule on Case has the formula AND(ISPICKVAL(Status, "Closed"), ISBLANK(Resolution_c)), the error location set to the Resolution_c field, and the Active box checked. A support agent edits an old Closed case that has no resolution and changes only the Subject. What happens, and why?
Show answer
The save is blocked and the error message appears under the Resolution_c field. The formula runs against the record as it would look after the save, so Status is still Closed and Resolution_c is still blank. The AND returns true, which means invalid. The rule does not care that the agent changed an unrelated field, and activating the rule did not recheck old records, so this is the first time the case is evaluated. The agent must fill in the resolution (or the rule must be changed) to save.