Sub topic 12
Enforcing stage requirements with validation rules and automation
Path and guidance for success tell reps what to do at each stage. They don't force anything. A rep can ignore the key fields and save the record anyway. Validation rules and automation close that gap by blocking bad data and by doing routine work for the rep.
This chapter assumes you can already do the following:
- Navigate Setup and the Object Manager.
- Describe how stages, sales processes, and record types fit together.
- Read a basic formula.
Why enforce rules at the stage level
A stage is a promise about a deal. "Negotiation/Review" means a buyer has seen terms. If a record reaches that stage with no next step, no amount, or no close date, then every report and forecast built on it is unreliable.
Stage-based enforcement works well for three reasons:
- Requirements grow as the deal matures. Early stages need little data. Late stages need a lot.
- Rules sit in one place and apply to every user, to imports, to API integrations, and to automation.
- Reps get a message at the moment they can fix the problem.
How a validation rule works
A validation rule is a formula that evaluates to true or false when someone saves a record. If the formula returns true, the save fails and Salesforce shows your error message. This is the opposite of what many people expect. You write the condition that describes bad data, not good data.
The rule runs on create and on edit. It runs for UI saves, imports, API calls, and flows that update the record. Rules do not run when a record is only viewed or when a field is changed in the browser without saving.
A first stage rule
This formula blocks saving an opportunity in the Negotiation/Review stage unless Next Step is filled in:
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
ISBLANK(NextStep)
)Walk through it:
ISPICKVAL(StageName, "Negotiation/Review")is true only when the stage equals that value. You cannot compare a picklist field with=. UseISPICKVALfor the comparison.ISBLANK(NextStep)is true when the field is empty. Next Step is a text field, soISBLANKis the right function.ANDreturns true only when both conditions hold. In that case the save is blocked.
When you create the rule in Setup > Object Manager > Opportunity > Validation Rules, you also set:
- Error message. Say what to do, for example "Enter a Next Step before moving to Negotiation/Review."
- Error location. Choose a specific field, such as Next Step, so the message appears next to the field. Choose Top of Page for rules that involve several fields.
Requiring data at several stages
Late stages normally share a requirement. Rather than writing a rule per stage, test several stages at once:
AND(
OR(
ISPICKVAL(StageName, "Proposal/Price Quote"),
ISPICKVAL(StageName, "Negotiation/Review"),
ISPICKVAL(StageName, "Closed Won")
),
Amount <= 0
)If Amount is blank on a number or currency field, comparison behavior can surprise you. Test blank values explicitly with ISBLANK(Amount) as well, or the rule may not fire the way you expect.
Rules that depend on the sales process
Different sales processes can contain different stage names. A rule that names a stage that does not exist in a given process never fires for that process, which is usually harmless. If you want a rule to apply to only one motion, include the record type in the formula:
AND(
RecordType.DeveloperName = "New_Business",
ISPICKVAL(StageName, "Closed Won"),
ISBLANK(Description)
)Using DeveloperName is safer than the record type name or ID. The developer name stays the same when you move the rule between sandboxes and production, and an admin who relabels the type cannot break it.
Useful functions for stage rules
| Function | Use |
|---|---|
| `ISPICKVAL(field, "value")` | Compare a picklist to one value |
| `ISBLANK(field)` | Test a field for empty |
| `ISNEW()` | True only while creating a record |
| `ISCHANGED(field)` | True when the field differs from its saved value |
| `PRIORVALUE(field)` | The value before the current edit |
| `TEXT(StageName)` | Convert a picklist to text for string comparison |
| `IsClosed`, `IsWon` | Booleans on the opportunity derived from the stage |
ISCHANGED is the most useful for stage logic, because it lets you fire a rule only on the transition and not on every later edit.
AND(
ISCHANGED(StageName),
ISPICKVAL(StageName, "Closed Lost"),
ISBLANK(TEXT(Loss_Reason__c))
)This rule assumes a custom picklist field named Loss_Reason__c. Substitute your own field. Because the rule checks ISCHANGED, a user can still edit other fields on an old closed record without being asked for a reason again.
Preventing backward moves
You can stop reps from moving a deal backward in the pipeline. This is a policy decision, so agree on it with sales leadership first. Some teams want reps to reopen deals freely. Others want an audit trail.
AND(
ISCHANGED(StageName),
PRIORVALUE(IsClosed) = true,
NOT($Permission.Reopen_Closed_Deals)
)This formula uses a custom permission called Reopen_Closed_Deals, which you must create and assign yourself. It lets a manager reopen a closed deal and blocks everyone else.
Validation rules versus the alternatives
Validation rules are not the only way to shape stage behavior.
| Option | What it does | Best for |
|---|---|---|
| Path key fields | Shows fields to fill in at a stage | Guiding, not blocking |
| Required field on layout | Requires a value at all times | Data that every deal needs from creation |
| Validation rule | Blocks a save when a condition is true | Conditional requirements by stage |
| Record-triggered flow | Changes or creates data after a save | Automating follow-up work |
A field that is required on the page layout is required for every stage, and only in the UI. A validation rule applies in every channel, and can depend on stage.
Automation triggered by stage
Validation stops bad saves. Automation handles what should happen after a good one. The tool to use is a record-triggered flow on Opportunity. Workflow rules and Process Builder are retired for new work, so build new automation in Flow.
Common stage-driven actions:
- Create a task for the owner when a deal enters Proposal.
- Notify a manager when a large deal reaches Closed Won.
- Set a field, such as a Closed Date stamp, when the stage changes.
- Create a renewal opportunity when a deal closes won.
Flow structure
A typical flow for "task when the deal reaches Proposal" is configured like this:
- Object: Opportunity. Trigger: a record is created or updated.
- Condition: Stage equals Proposal/Price Quote.
- Option: run only when the record is updated to meet the condition requirements. Without this option, the flow runs every time anyone edits a record in that stage.
- Optimize for: Actions and Related Records.
- Action: Create Records, with a Task whose Related To is the opportunity and whose owner is the opportunity owner.
The "only when updated to meet conditions" option is the most common source of duplicate tasks. If you leave it off, each edit to a deal in that stage creates another task.
Before-save flows run before validation, so they can fix or populate fields that a validation rule then checks. After-save flows run after the record is committed, so they cannot rescue a record that failed validation.
Common errors and gotchas
- Writing the rule backward. A rule that describes good data blocks every good save. Always state the condition for failure.
- Comparing a picklist with
=. This produces a syntax error. UseISPICKVAL. - Blanket rules that break integrations. A rule firing on every closed deal can reject records loaded by an integration user or a data load. Exempt those users with a condition like
$User.Username <> "[email protected]", or use a custom permission. Do this deliberately. - Rules that trap old data. A new rule applies to existing records at their next save. If thousands of old deals fail the new condition, users who edit an unrelated field are blocked. Use
ISCHANGEDor restrict by date to avoid this. - Lead conversion failures. Conversion creates an opportunity, and validation rules run on it. A rule that requires a field the conversion screen does not fill in will make conversion fail with the rule's error message.
- Too many rules. Each required field adds friction. Reps who feel blocked start entering junk values such as "TBD" in Next Step. Require only what the next person in the process needs.
- Flows that loop. A flow that updates the same opportunity can trigger itself. Use entry conditions that become false after the update.
- Hidden fields. If a rule requires a field that is not on the user's layout or Path, they cannot fill it in. Check that every field named in a rule is visible to the people it affects.
Testing a new stage rule
- Write the formula and click Check Syntax.
- Save the rule with the Active box cleared.
- Activate it in a sandbox.
- Try to save a record that should fail, then one that should pass.
- Try an edit on an old closed record to check for side effects.
- Deploy and activate in production.
Exercise
A team wants every opportunity that moves to "Closed Lost" to have a value in a custom picklist field named LossReason_c. They do not want reps on older closed-lost deals to be blocked when editing other fields. Write the validation formula and explain why it uses ISCHANGED.
Show answer
Formula: AND(ISCHANGED(StageName), ISPICKVAL(StageName, "Closed Lost"), ISBLANK(TEXT(LossReason_c))). The rule is true, and so blocks the save, only when the stage has just been changed to Closed Lost and the loss reason is empty. Without ISCHANGED, any later edit to a closed-lost deal that has no loss reason would fail, which would block users who are only updating another field on an old record.
Exercise
A record-triggered flow creates a follow-up task whenever an opportunity is in the Proposal stage. Reps report getting five identical tasks for one deal. What is the likely cause and the fix?
Show answer
The flow runs on every create or update of a record that meets the condition, so each edit while the deal sits in Proposal creates another task. Set the trigger option to run only when a record is updated to meet the condition requirements, so the flow fires once, on the change into Proposal.