Sub topic 1
What validation rules do and when they fire
Prerequisites
This chapter assumes you know the following:
- The basic Salesforce data model: objects, fields, and records.
- How to open Setup and the Object Manager.
- How to read a simple spreadsheet-style formula, such as
IF(A1 > 0, "yes", "no").
You do not need Apex for this chapter. Apex appears only in the later chapter on testing.
What a validation rule is
A validation rule is a check that Salesforce runs when a record is saved. The rule contains a formula. If the formula evaluates to true, Salesforce refuses the save and shows an error message that you wrote. If the formula evaluates to false, the save continues as normal.
This is the opposite of what many people expect. The formula describes the bad data, not the good data. A rule that says "Amount must be positive" is written as Amount <= 0, because that expression is true when the data is wrong.
Validation rules exist to keep data quality high at the point of entry. Fixing a bad record later is far more expensive than stopping it from being saved. Typical uses include:
- Requiring a field only when another field has a certain value, such as an Amount once an Opportunity reaches a late stage.
- Checking a format, such as a postal code.
- Blocking changes to a field by users who are not allowed to make them.
- Comparing two fields, such as an end date that must not be before a start date.
A first example
This rule is attached to the Opportunity object. It returns true, and so blocks the save, when the stage is Negotiation/Review and Amount is blank or not positive.
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)Read it as a sentence: "The record is invalid when the stage is Negotiation/Review and the Amount is either empty or zero or less." Both parts of AND must be true for the error to appear. An Opportunity at any other stage passes, because the first condition is false.
Later chapters cover each function in this formula. For now, focus on the direction of the logic: true means invalid.
When validation rules fire
A rule runs when a user or process attempts to save a record. The save may come from many sources:
- A user clicking Save on a record page in Lightning Experience.
- An edit from a list view with inline editing.
- A data load through Data Import Wizard, Data Loader, or the Bulk API.
- An API call from an integration.
- Apex code that runs a DML statement such as
insertorupdate. - A flow that creates or updates records.
- A record change that arises from a process, such as lead conversion.
Validation rules are not tied to the user interface. A rule fires for all of these sources unless you build a bypass (covered in a later chapter). This is a strength, because the rule protects data no matter how it arrives. It is also a common source of surprises. A rule that suits a person typing in a form can break a nightly integration that writes partial data.
Which operations trigger a rule
When you create a rule, you do not choose between insert and update. The rule runs on both. If you want it to run only on creation or only on edits, you add that logic to the formula yourself, using functions such as ISNEW() and ISCHANGED(). A later chapter covers these.
Rules do not run when a record is deleted. They also do not run when you merely view a record.
The active flag
Each rule has an Active checkbox. An inactive rule is stored but never evaluated. Deactivating is the safe way to turn a rule off temporarily, for example during a one-time data migration, because the formula stays available for reactivation.
Where a rule runs in the save process
Salesforce does not just write a record to the database. It runs a sequence of steps called the order of execution. Validation rules sit in the middle of that sequence. A later chapter covers the full order. The part you need now is this:
- Salesforce loads the new field values onto the record.
- Before-save flows and before triggers can change field values.
- System validation runs, such as required fields and field format checks.
- Custom validation rules run.
- If everything passes, the record is saved to the database, though not yet committed.
- After-save logic runs, such as after triggers and after-save flows.
Two consequences follow from this placement.
- Validation rules see values after before-save automation has run. If a before-save flow fills in a field, your rule sees the filled-in value, not what the user typed.
- Validation rules run before after-save automation. If a rule blocks the save, after-save flows and triggers for that record do not run, and nothing is written.
Anatomy at a glance
Every validation rule has the same parts. A later chapter treats each in detail.
| Part | Purpose |
|---|---|
| Rule name | A unique API-style name, with no spaces |
| Active | Whether the rule is evaluated |
| Description | Notes for other admins about the intent |
| Error condition formula | The expression that is true when data is invalid |
| Error message | The text shown to the user |
| Error location | The top of the page, or next to a specific field |
Write a description for every rule. Months later, nobody remembers why a formula looks the way it does.
What a rule can and cannot see
A formula can read fields on the record being saved. It can also read fields on records reached through lookup or master-detail relationships, such as Account.Industry from an Opportunity. It can read some global variables, such as $User, $Profile, and $Permission.
A rule cannot query other records. It cannot ask "how many Contacts does this Account have?" or "is there another Opportunity with the same name?" Those requirements need a roll-up summary field, a flow, or Apex. A later chapter on alternatives covers the decision.
Blank values and rule behavior
A rule runs on the full record, not only on the fields a user changed. If an existing record already violates a rule, the next edit to any field on it will fail, even if the user did not touch the offending field. Users often report this as "I can't save my change, and the error is about a field I didn't edit."
Two consequences matter when you add a rule to an object that already holds data:
- Existing records are not checked at the moment you activate the rule. Salesforce only checks a record when it is next saved.
- Records that already violate the rule become blocked from unrelated edits until someone fixes them.
Before activating a new rule, report on the existing data to find records that would fail. Fix them first, or write the formula to apply only to new records or changed values.
Common mistakes at this stage
- Writing the formula as the valid condition. If your rule blocks every good record and passes every bad one, you have inverted it. Wrap the formula in
NOT()or restate it. - Forgetting integrations. A rule fires for API users. Check what your integrations send before activating.
- Assuming the rule runs only in the UI. Apex, flows, and data loads all trigger it.
- Expecting a rule to run on delete. It does not.
- Assuming a rule can look at other records. It cannot query.
Summary of the model
- A validation rule is a formula attached to an object.
- When the formula is true, the save is blocked and the message is shown.
- The rule runs on every insert and update, from every source, after before-save automation and before after-save automation.
- It evaluates the whole record, so old bad data can block new edits.
- It cannot query other records.
Exercise
A rule on Opportunity has the formula Amount > 0, intended to require a positive Amount. After you activate it, users can save Opportunities with a blank Amount, but they cannot save any Opportunity with an Amount of 5000. What went wrong, and what is the corrected formula?
Show answer
The formula is written as the valid condition, but a validation rule blocks the save when the formula is true. So it blocks every positive Amount. The formula should describe the invalid case: Amount <= 0 blocks zero and negative values. A blank Amount also needs handling, because comparing a blank number may not behave as you expect, so use OR(ISBLANK(Amount), Amount <= 0) to block blanks as well.
Exercise
A before-save flow sets the Region field to "West" whenever the State is "CA". A validation rule on the same object blocks the save when Region is blank. A user saves a record with State "CA" and Region left empty. Does the save succeed? Explain using the order of execution.
Show answer
The save succeeds. Before-save flows run before custom validation rules, so the flow fills in Region with "West" first. The validation rule then sees "West", the formula is false, and the save continues.