Sub topic 8
Validating only on create or on change
This chapter assumes you can write a basic error condition formula, that you know ISBLANK and ISPICKVAL, and that you know a rule blocks the save when its formula returns true. It covers how to make a rule fire only when a record is created or when a specific field changes.
Why a rule needs a timing condition
By default, a validation rule runs on every save, both inserts and updates. That causes two common problems:
- Old records block unrelated edits. Suppose you add a rule that requires
Descriptionto be filled in. Every existing record with an empty description now fails when someone edits any field on it. Users are blocked for a problem they did not cause. - Rules are too strict after a point in the lifecycle. A rule such as "Amount must be positive" is reasonable. A rule such as "Amount cannot change once the deal is Closed Won" only makes sense on updates, and only when
Amountis the field being edited.
The fix is to add a timing condition. Three functions provide it: ISNEW, ISCHANGED, and PRIORVALUE.
The three functions
| Function | Returns | Typical use |
|---|---|---|
| `ISNEW()` | True when the record is being inserted | Rules that apply only at creation |
| `ISCHANGED(field)` | True when the field's new value differs from the value saved in the database | Rules that apply only when a user edits that field |
| `PRIORVALUE(field)` | The value the field had before this save | Comparing the old and new value, or checking the old state |
All three are available in validation rules and a few other automation formulas. They are not available in ordinary formula fields.
ISNEW
ISNEW() takes no argument. It is true during an insert and false during an update. Use it to require a field only at creation:
AND(
ISNEW(),
ISBLANK(Description)
)Records that already exist are untouched, even if their description is empty. If the rule should apply only to updates, negate it with NOT(ISNEW()).
ISCHANGED
ISCHANGED(field) compares the incoming value with the stored one. It lets you scope a rule to one field, so edits to other fields are never blocked.
This rule requires a loss reason, but only at the moment a user moves the stage to Closed Lost. It assumes a custom text field named Loss_Reason__c:
AND(
ISCHANGED(StageName),
ISPICKVAL(StageName, "Closed Lost"),
ISBLANK(Loss_Reason__c)
)A user who edits the Next Step field on an already-closed opportunity is not affected, because StageName did not change.
Pay attention to what ISCHANGED does on inserts. A new record has no stored value to compare against, so do not depend on it for creation logic. State the intent explicitly with ISNEW(). The example in the spine does this:
AND(
NOT(ISNEW()),
ISCHANGED(Amount),
NOT($Permission.Can_Edit_Opportunity_Amount)
)Read it as: this is an existing record, Amount is being edited, and the user lacks the permission to do that. All three must be true for the save to be blocked.
PRIORVALUE
PRIORVALUE(field) returns the old value. Use it when you care about what the field used to be, not only whether it changed. This rule stops edits to Amount once an opportunity was already Closed Won before this save:
AND(
NOT(ISNEW()),
ISPICKVAL(PRIORVALUE(StageName), "Closed Won"),
ISCHANGED(Amount)
)Note the use of the prior stage, not the current one. If the rule checked the current StageName, a user could change the stage and the amount in a single save and slip past it. Checking the prior value closes that gap.
Picklists and prior values
Picklist fields need ISPICKVAL, and that includes the prior value. Write ISPICKVAL(PRIORVALUE(StageName), "Closed Won"). A comparison such as PRIORVALUE(StageName) = "Closed Won" is an error, because picklist values cannot be compared as plain text.
ISCHANGED works on picklists directly: ISCHANGED(StageName) needs no wrapper. You can combine it with ISPICKVAL to test the new value, as in the Closed Lost example above.
Combining change detection with blank handling
Change detection often pairs with blank checks, and the order of thinking matters. Ask these two questions separately:
- When should this rule be eligible to fire? (insert, update, or a particular field change)
- What data state is invalid at that moment?
Put the answer to the first question in the leading clauses and the second in the trailing ones. For example, to block clearing a phone number on an existing contact:
AND(
NOT(ISNEW()),
ISCHANGED(Phone),
ISBLANK(Phone)
)The rule fires only when the phone number was edited and the new value is empty. A contact that never had a phone number can still be saved, because Phone did not change.
Deciding which function to use
Edge cases and common errors
- Formula fields and cross-object fields.
ISCHANGEDandPRIORVALUEwork on fields of the record being saved. They do not work on formula fields, and you cannot apply them to a related record's field such asAccount.Industry. If you need to react to a parent change, validate on the parent object or use a flow or Apex instead. - Changes made by automation. Validation rules run after before-save triggers and before-save flows. If automation modifies a field during the save, the validation rule sees that field as changed. A user who never touched
Amountcan still trigger anISCHANGED(Amount)rule if automation edits it. When a rule fires unexpectedly, check for automation writing to the field. - Same-value saves. If a user re-enters the same value,
ISCHANGEDis false. The field must differ from the stored value. - Data loads and integrations. Bulk updates often send every column, but only columns whose values differ count as changed. A load that rewrites unchanged values does not trip an
ISCHANGEDrule. Rules that useISNEWstill fire on inserted rows, so use the bypass pattern from a later chapter for loads. - Case and null differences. A field changing from null to an empty-looking value is not a visible change in most cases. If a rule behaves oddly, test with the exact before and after values rather than assuming.
- Inserts and
PRIORVALUE. A new record has no prior value. Guard prior-value logic withNOT(ISNEW())so the intent is clear and the rule cannot misfire on creation.
Testing the timing
Test each rule in at least three ways: an insert that should fail, an update to the watched field that should fail, and an update to a different field on an existing record that should succeed. The third test is the one people skip, and it is the one that proves the timing condition works. A rule that blocks the unrelated edit is still too broad.
Exercise
Write a validation rule formula for Opportunity that prevents anyone from changing CloseDate on an existing opportunity whose stage was already Closed Won before the save. Inserts must never be blocked. Why is PRIORVALUE(StageName) used instead of StageName?
Show answer
AND(NOT(ISNEW()), ISPICKVAL(PRIORVALUE(StageName), "Closed Won"), ISCHANGED(CloseDate)). The prior value is used so that a user cannot change the stage to something else and edit CloseDate in the same save to avoid the rule. The rule looks at the state the record was in before the save. NOT(ISNEW()) keeps it from firing on inserts, and ISCHANGED(CloseDate) means edits to other fields are still allowed.
Exercise
A rule requires Industry on every Account, using AND(ISPICKVAL(Industry, ""), TRUE-style checks aside, no timing condition). After deployment, users complain they cannot update the phone number on old accounts. How do you change the rule so it only affects new accounts and anyone who edits Industry?
Show answer
Add timing conditions. For new records only: AND(ISNEW(), ISPICKVAL(Industry, "")). To also cover edits that clear the field: OR(ISNEW(), ISCHANGED(Industry)) combined with ISPICKVAL(Industry, ""), written as AND(OR(ISNEW(), ISCHANGED(Industry)), ISPICKVAL(Industry, "")). Old accounts with an empty Industry can then be saved when the user edits only the phone number.