Skip to content

Automation

Validation Rules in Salesforce

Stopping incorrect input with rules that Salesforce checks every time a record is saved.

Reading time
5 min
Published
Updated
All articles

What is a validation rule?

A validation rule is a condition (formula) that checks whether entered data meets certain criteria. When that formula returns TRUE, the input is considered invalid and Salesforce prevents the record from being saved.

In other words: the validation rule verifies the entered data and blocks saving if it doesn't meet the standard. Users then see an error message explaining which field values are invalid.

How does it work in practice?

When a user saves a new record or updates an existing one, all active validation rules for that object are checked. If all data is valid (none of the rules return TRUE), the save proceeds normally.

But if one or more validation rules return TRUE (meaning they find invalid input), then:

  • The save is cancelled and the record remains unchanged
  • The user immediately sees an error message
  • The user must correct the input and save again

The two components of a validation rule

A validation rule consists of two important components:

1. The formula

The formula describes the condition that should not be true. For example: "Completion date must not be in the future". If this condition is true anyway, the rule blocks the save.

2. The error message

The text the user sees when the rule triggers. For example: "Completion date cannot be in the future." You can choose whether this appears at the top of the page or next to a specific field.

Note

Validation rules are always active, regardless of whether a field is on the page layout or not. A rule that checks a hidden field will still block input if the condition is met.

Object validation rules vs. validation in Flows

Salesforce offers two ways to validate data: at the object level and in Screen Flows. While the goal is similar, the implementation differs.

Validation rule on the object

Created via Setup > Object Manager. Checks the record data after someone clicks Save.

  • Checks on save (UI, API, import)
  • Applies to new and existing records
  • Valid for entire organization

TRUE means here: the input is invalid and Salesforce blocks the save.

Validation in a Screen Flow

Set via Validate Input on a field component in Flow Builder.

  • Checks immediately while filling in
  • Direct visual feedback for user
  • Only active within the specific Flow

TRUE means here: the input is valid and the user can continue.

The key difference: the logic is reversed

For objects you write the formula as "when is the input invalid?" (TRUE causes error). For flows you write the formula as "when is the input valid?" (FALSE causes error).

In the interactive generator below, you can indicate whether you want to generate a rule for an object or for a flow. The tool automatically reverses the logic for flow validations, so you can use the formula directly.

Interactive generator for validation rules

Use the tool below to build a validation rule formula step by step. This is especially useful if you don't write formulas regularly and want to be sure the syntax is correct.

Validatieregel Generator

Conditie 1
Conditie 2
Conditie 3

Tips voor gebruik:

  • Gebruik API-namen van velden (bijv. Account.Name, Custom_Field__c)
  • Voor datums: typ TODAY voor vandaag, of 2025-12-31 voor een vaste datum
  • Decimalen met punt schrijven (bijv. 3.5)
  • Vergelijken met ander veld? Vul de API-naam in als waarde

Applying the generated validation rule in Salesforce

As an object validation rule (when saving records)

If you've created a formula for an object with the tool, add it to Salesforce as follows:

  1. Go to Setup. Navigate to Object Manager and select the object where you want to create the rule (for example Account, Contact or a custom object).
  2. Open Validation Rules. Click Validation Rules in the object menu. You'll see an overview of existing rules.
  3. Create a new rule. Click New to add a new validation rule.
  4. Fill in the fields. Configure the rule:
    • Rule Name: A clear name (e.g. "Account_Number_8_Characters")
    • Active: Check to make the rule immediately active
    • Description: Briefly describe what the rule does
    • Error Condition Formula: Paste the generated formula here
    • Error Message: The text the user sees
    • Error Location: At the top of the page or next to a specific field
  5. Check and save. Click Check Syntax to validate the formula, then save with Save.
Practical example

Suppose you create a rule on Opportunity with the formula:

AND(ISPICKVAL(StageName, "Closed Won"), ISBLANK(Project_Start_Date__c))

This rule requires a custom field "Project Start Date" to be filled in once the opportunity reaches the "Closed Won" stage. If a user tries to save an Opportunity as Closed Won without a start date, the error message appears.

As validation in a Flow screen (Screen Flow)

When you've generated a formula for Flow, you can use it in a Screen Flow:

  1. Open the relevant Flow in the Flow Builder
  2. Select the screen containing the field you want to validate
  3. Click on the field component to view its properties
  4. Find the section Validate Input
  5. Paste the generated formula in the formula field
  6. Under Error Message enter the text the user sees
  7. Save and test the flow

Common formula functions

Salesforce offers many functions for formulas. Here are the most commonly used for validation rules:

ISBLANK(field)

Checks if a field is empty (contains no value). Use this instead of comparing to an empty string.

ISBLANK(Email) → TRUE if Email is empty

ISPICKVAL(picklist_field, text)

Checks if a picklist field has a certain value. Use this instead of comparing the field directly to text.

ISPICKVAL(Status__c, "Active") → TRUE if Status__c = "Active"

INCLUDES(multipicklist_field, text)

For multi-select picklists: checks if a certain option is selected.

INCLUDES(Interests__c, "Sports") → TRUE if "Sports" is selected

AND() and OR()

Combine multiple conditions. Use AND if all conditions must be true, or OR if one of multiple conditions is sufficient.

AND(ISBLANK(Phone), ISBLANK(Email)) → TRUE if both are empty
OR(ISBLANK(Phone), ISBLANK(Email)) → TRUE if either is empty

REGEX(text, regex_pattern)

Checks if a text field matches a certain pattern. Useful for format checks such as postal codes or email addresses.

NOT(REGEX(PostalCode__c, "[0-9]{4}[A-Z]{2}")) → Error if postal code doesn't have NL format

Best practices for validation rules

Write clear error messages

The end user sees the error message, not the formula. Avoid jargon and explain what's wrong and how to fix it. For example: "Date cannot be in the future" is clearer than "Invalid date".

Test thoroughly

Try different scenarios: both data that should be accepted and data that should be blocked. A poorly designed validation rule can prevent users from saving valid data too.

Introduce gradually

It's not advisable to deploy many new validation rules to production at once. Better to do it step by step, so you can handle any unexpected effects. Inform users about new rules.

Document your rules

Give each rule a clear name and description in Salesforce. That way anyone who later handles functional management of Salesforce, you or a fellow admin, understands what the rule was meant to do.

Consider integrations and imports

Validation rules always run when a record is saved, regardless of who makes the change or how (UI, data load, API). Keep this in mind with integrations with other systems and imports: rules can trigger then too, and the data must meet the conditions you set.

Common validation scenarios

  • Required fields by status: Project start date required when Opportunity moves to "Closed Won".
  • Date validation: End date must not be before start date, or close date must not be in the future.
  • Format checking: Postal code must be 4 digits + 2 letters, or phone number must start with +31.
  • Value range: Discount percentage must be between 0 and 30%, or amount must be at least €100.
  • Contact info required: At least phone or email must be filled in on a Contact record.
  • Picklist dependency: Reason field required when status is set to "Rejected".

Start with simple rules

Validation rules stop incorrect input at the moment someone saves a record. That way you catch errors at the source, and data is entered the same way throughout your organization.

With our interactive generator, you can quickly build the right formula without having to memorize exact syntax. Start with simple rules and build out gradually as you gain more experience.

You can read more about automation in Salesforce in Automating with Salesforce Flow. In the article on the Order of Execution you can see when Salesforce runs validation rules.

Need help setting up validation rules?

We help you set up validation rules that stop incorrect input, with error messages users understand.

Schedule a conversation