Sub topic 8
Validating only on create or on change
This chapter assumes you can write a basic error condition formula, where true means the data is invalid, and that you know how to handle blank values with ISBLANK. It covers how to limit a rule to new records or to specific edits.
Why a rule needs to know about the edit
A validation rule runs on every insert and update. Often that is too broad. You may want to enforce a value only when a record is created, or only when a particular field changes. Without that limit, a rule can block users from editing unrelated fields on old records that already break the rule.
Salesforce gives you three tools for this:
ISNEW()returns true when the record is being created.ISCHANGED(field)returns true when the field's value differs from the saved value.PRIORVALUE(field)returns the value the field had before this edit.
On insert, ISNEW() is true, and ISCHANGED is always false.
Only on create
Use ISNEW() when a condition matters only at creation. This rule requires a close date in the future for new Opportunities:
AND(
ISNEW(),
CloseDate < TODAY()
)Existing records with past close dates can still be edited, because ISNEW() is false for them.
Only on change
Use ISCHANGED to block edits to a field. The rule below stops anyone without a custom permission from changing Amount on an existing record:
AND(
NOT(ISNEW()),
ISCHANGED(Amount),
NOT($Permission.Can_Edit_Opportunity_Amount)
)NOT(ISNEW()) is included for clarity. ISCHANGED is already false on insert, so the rule would behave the same without it. Create the custom permission first, or the formula will not save.
Comparing old and new values
ISCHANGED only tells you that something changed. PRIORVALUE lets you inspect how. This rule prevents Amount from being reduced after the record is saved:
AND(
NOT(ISNEW()),
ISCHANGED(Amount),
Amount < PRIORVALUE(Amount)
)PRIORVALUE is also an error on a new record, so always guard it with NOT(ISNEW()), or with ISCHANGED, which already excludes inserts.
Picklists and change detection
ISCHANGED works on picklist fields without ISPICKVAL. To react to a move into a specific stage, combine both:
AND(
ISCHANGED(StageName),
ISPICKVAL(StageName, "Closed Won"),
ISBLANK(Description)
)This fires only on the edit that moves the stage to Closed Won, not on later edits to a record that is already there.
Common errors
- Using
PRIORVALUEon insert. Guard it withNOT(ISNEW()). - Expecting
ISCHANGEDto catch creation. A new record has no prior value, so it never counts as changed. UseISNEW()for that. - Blocking old data. A rule without a change check can stop users from saving any edit to a legacy record that already violates it. Add
ISCHANGEDon the relevant field if you only care about new changes. - Cross-object fields.
ISCHANGEDandPRIORVALUEdo not work on fields of related records, such asAccount.Industry.
Exercise
Write an error condition formula for Opportunity that blocks a change to CloseDate on an existing record when the new date is earlier than the previous date.
Show answer
AND(NOT(ISNEW()), ISCHANGED(CloseDate), CloseDate < PRIORVALUE(CloseDate)). The NOT(ISNEW()) guard keeps PRIORVALUE from being evaluated on insert, and ISCHANGED limits the rule to edits that touch CloseDate.