Salesforce Validation Rules and Formula Fields
Data quality is one of the biggest challenges in any CRM. Users make typos, skip required fields, or enter values in the wrong format. Salesforce gives administrators two powerful tools to maintain clean, reliable data: Validation Rules that prevent bad data from being saved, and Formula Fields that calculate values automatically. Neither requires coding — both use Salesforce's formula language.
What Are Validation Rules?
A Validation Rule checks field values when a user tries to save a record. If the rule's condition is true — meaning the data is invalid — Salesforce blocks the save and shows an error message. The user must correct the data before the record can be saved.
The Airport Check-In Analogy
AIRPORT CHECK-IN = Validation Rule ───────────────────────────────────── System checks: Does passport match ticket name? If NO → "Please correct your name before proceeding." User corrects → System allows check-in. SALESFORCE EQUIVALENT: Rule checks: Is Close Date in the past? If YES → "Close Date cannot be in the past. Please update." User corrects → Record saves.
How Validation Rules Work
Every validation rule has two parts:
- Error Condition Formula — a formula that evaluates to TRUE when the data is invalid
- Error Message — what users see when the rule fires
The rule fires (blocks the save) when the formula returns TRUE. So you write the formula to describe the bad data state, not the good one.
Common Validation Rule Examples
Prevent a Past Close Date on Opportunities
Error Condition: CloseDate < TODAY() Error Message: "Close Date cannot be in the past." Error Location: CloseDate field
Require a Phone Number When Stage Is Closed Won
Error Condition: AND(
ISPICKVAL(StageName, "Closed Won"),
ISBLANK(Phone)
)
Error Message: "A phone number is required when closing a deal."
Error Location: Phone field
Validate Email Format
Error Condition: NOT(REGEX(Email, "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"))
Error Message: "Please enter a valid email address."
Error Location: Email field
Validation Rule Best Practices
- Write error messages in plain language that tells the user exactly what to fix.
- Place the error on the specific field that needs correction — not just at the top of the page.
- Test every validation rule with both valid and invalid data before activating it.
- Avoid creating rules so strict that they block legitimate data entry and frustrate users.
- Use the
ISCHANGED()function when a rule should only fire when a specific field changes.
Useful Validation Rule Functions
| Function | What It Does | Example Use |
|---|---|---|
| ISBLANK() | Returns TRUE if the field is empty | Require a field to be filled in |
| ISPICKVAL() | Checks if a picklist equals a specific value | Validate based on stage or status |
| TODAY() | Returns today's date | Prevent past dates |
| LEN() | Returns the character count of a text field | Enforce a minimum or maximum length |
| REGEX() | Matches a text pattern | Validate phone numbers, postal codes |
| AND() | All conditions must be true | Multiple conditions together |
| OR() | At least one condition must be true | Check multiple invalid states |
| ISCHANGED() | Returns TRUE when a field value changes | Prevent reversing a stage |
What Are Formula Fields?
A Formula Field is a read-only field whose value Salesforce calculates automatically based on other fields. You write a formula once, and Salesforce applies it to every record — always showing the current calculated value. The user never types into a formula field.
The Spreadsheet Cell Analogy
EXCEL CELL WITH FORMULA: =B2 * C2 SALESFORCE FORMULA FIELD: Amount * Discount_Percent__c / 100 Both calculate automatically. Both update when source values change. Neither requires manual entry.
Formula Field Examples
Days Until Close (Opportunity)
Formula: CloseDate - TODAY()
Result: Shows how many days remain until the deal's close date.
Automatically updates every day.
Full Name (Contact)
Formula: FirstName & " " & LastName Result: "Priya Sharma" — combines two text fields into one.
Discounted Amount (Opportunity)
Formula: Amount * (1 - Discount_Percent__c / 100) Result: Calculates the final price after discount.
Account Tier Label (Account)
Formula: IF(AnnualRevenue >= 10000000, "Enterprise",
IF(AnnualRevenue >= 1000000, "Mid-Market", "SMB"))
Result: Labels each account as Enterprise, Mid-Market, or SMB
based on their revenue — automatically.
Formula Field Return Types
When you create a formula field, you must choose what type of value the formula returns:
- Text — a word or phrase
- Number — a numeric result
- Currency — a money value
- Date — a calendar date
- Date/Time — a date with time
- Percent — a percentage value
- Checkbox — a true/false result
Cross-Object Formulas
Formula fields can reference fields on related objects using the relationship name. For example, on a Contact record, you can display the parent Account's Annual Revenue:
Formula on Contact: Account.AnnualRevenue Result: Shows the Account's revenue directly on the Contact record.
This eliminates the need to open the Account record just to check one value — the Contact page shows it directly.
Key Points
- Validation Rules block record saves when data does not meet your defined criteria — protecting data quality.
- Write validation formulas to describe the invalid state (the rule fires when the formula is TRUE).
- Formula Fields calculate values automatically from other fields — users never type into them.
- Both validation rules and formula fields use Salesforce's formula language with functions like IF(), AND(), ISBLANK(), and TODAY().
- Cross-object formulas let a field on one object display data from a related object.
