Business Logic and Process Automation · Topic 3 of 7

Validation Rules

Salesforce validation rules: error condition formulas, error messages and locations, ISCHANGED, PRIORVALUE, ISNEW, REGEX, and when rules run on save.

  • 4 min
In a nutshell
  • The formula describes invalid data; true blocks the save.
  • Error messages can appear at the top of the page or next to a field.
  • ISCHANGED, PRIORVALUE and ISNEW handle change-based rules.

What it is

A validation rule checks data when a record is saved. It contains an error condition formula: if the formula is true, the save is blocked and the user sees an error message. If it is false, the record saves.

Validation rules are the declarative way to enforce data quality, such as "a closed opportunity must have a reason" or "discount cannot exceed 30%".

User saves recordValidation ruleformula evaluatestrueError shownsave blockedUser fixes data

How it works

Parts of a validation rule

PartWhat you set
Rule name and descriptionWhat the rule checks
Error condition formulaReturns true when the data is invalid
Error messageThe text the user sees
Error locationTop of the page, or next to a specific field
ActiveOnly active rules run

The key habit is to write the formula for the bad case. "Amount must be positive" becomes Amount <= 0.

Useful functions

FunctionUse
ISBLANKRequire a value, for example a reason when the stage is Closed Lost
ISPICKVALCheck a picklist value
ISCHANGEDCheck whether a field changed in this save
PRIORVALUECompare with the value before this save
ISNEWApply a check only when the record is created
REGEXMatch a pattern, such as a postcode format
VLOOKUPCheck a value against records in a custom object
$User, $Profile, $PermissionExempt certain users, profiles or custom permissions

A typical rule: the stage cannot move back from Closed Won.

ISCHANGED(StageName) && ISPICKVAL(PRIORVALUE(StageName), "Closed Won")

When validation rules run

  • On every save from the user interface, the API, imports, Data Loader and automation that updates the record.
  • Before the record is committed, after system validation such as required fields.
  • Record-triggered flows that run before save run before validation rules, so the rule checks their result.

Because rules apply to every source, an integration or a large data load can fail if it does not meet them. A common approach is to exempt an integration user with a custom permission checked in the formula.

Validation rules versus other tools

NeedTool
A field must always have a valueRequired field setting, or a validation rule when it depends on other values
Data must follow a rule based on other fieldsValidation rule
Fix data automatically instead of blockingBefore-save record-triggered flow
Restrict which picklist values can be chosenDependent picklists or record types

A worked example: a company requires a Loss Reason whenever an opportunity is set to Closed Lost, and the integration user must be exempt. The builder writes ISPICKVAL(StageName, "Closed Lost") && ISBLANK(Loss_Reason__c) && NOT($Permission.Bypass_Validation), sets the error to appear next to the Loss Reason field, and assigns the custom permission to the integration user through a permission set.

Exam tip

Read the requirement as "block the save when...". Whatever follows "when" is your formula.

How the exam asks about this

  • "Prevent users from changing a field after the record is approved." ISCHANGED with a status check.
  • "Allow administrators to bypass the rule." A $Profile or $Permission check in the formula.
  • "Ensure a field follows a pattern." REGEX.

What to remember for the exam

  • The formula describes invalid data; true blocks the save.
  • Error messages can appear at the top of the page or next to a field.
  • ISCHANGED, PRIORVALUE and ISNEW handle change-based rules.
  • Rules apply to all saves, including imports and the API.
  • Exempt users with $Profile, $User or a custom permission.
Watch out

A new validation rule also applies to existing records the next time they are edited, which can block unrelated updates and data loads.

Common mistakes

Writing the formula for the valid case. The formula must be true when data is wrong.

Forgetting integrations and imports. They are blocked too unless exempted.

Using a validation rule to correct data. Validation rules only block; use a flow to change values.

Check yourself

  • What happens when a validation rule's formula returns true?

    The save is blocked and the error message is shown.

  • Which function compares a field with its value before the current save?

    PRIORVALUE.

  • How can an integration user be exempted from a validation rule?

    Check a custom permission, profile or user in the formula and grant it to that user.

Topic 3 of 7 in Business Logic and Process Automation

Test yourself on Business Logic and Process Automation

The full Salesforce Platform App Builder question bank has 648 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Salesforce. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.