Data quality is the backbone of any successful Salesforce implementation. Poor data quality leads to inaccurate reports, frustrated users, and missed business opportunities. While Apex triggers have traditionally been the go-to solution for complex data validation, Salesforce Lightning Flow now offers a powerful, declarative alternative that admins can configure without writing a single line of code.
In this comprehensive guide, we'll explore how to build robust data validation automations using Lightning Flow, covering everything from basic field validations to complex cross-object data integrity checks.
Before diving into the how‑to, let's understand why Flow is an excellent choice for data validation:
While validation rules are perfect for simple field‑level checks, Flow excels when you need to perform complex validations involving multiple records, cross‑object queries, or conditional logic that depends on related data.
The key to effective data validation with Flow is using Record‑Triggered Flows with the Before Save timing. This executes your validation logic before the record is committed to the database, preventing invalid data from being saved.
Key characteristics:
Business Requirement: When a Contact is created or updated with an Account Type of "Business", the email domain must not be a free email provider (Gmail, Yahoo, Hotmail).
Step 1: Create a Record‑Triggered Flow
Contact | Trigger: created or updated | Optimize: Fast Field Updates | Trigger: Before the record is saved.Step 2: Add Entry Criteria – Decision element: {!$Record.AccountId} Is Null = False AND {!$Record.Email} Is Null = False.
Step 3: Get the Related Account – Get Records element: object Account, filter Id equals {!$Record.AccountId}, store first record in variable varAccount.
Step 4: Check Account Type – Decision: "Is Business Account" → condition {!varAccount.Type} Equals "Business".
Step 5: Validate Email Domain – First create a formula resource formulaEmailDomain (text) with:
LOWER(
RIGHT(
{!$Record.Email},
LEN({!$Record.Email}) - FIND("@", {!$Record.Email})
)
)
Then another Decision: Invalid Domain → conditions {!formulaEmailDomain} Contains "gmail.com" OR … "yahoo.com" OR … "hotmail.com".
Step 6: Error handling (validation‑rule workaround) – In Before‑Save you cannot add errors directly. Instead, create a checkbox field Flow_Validation_Error__c on Contact. In the "Invalid Domain" path, add an Assignment: {!$Record.Flow_Validation_Error__c} = TRUE. Then create a validation rule on Contact: Flow_Validation_Error__c = TRUE with error message: "Business contacts must use a corporate email address. Free email providers (Gmail, Yahoo, Hotmail) are not allowed."
Business Requirement: Prevent creating an Opportunity if another Opportunity exists for the same Account with Close Date within 30 days and the same Product Category.
Implementation highlights:
formulaStartDate = {!$Record.CloseDate} - 30 ; formulaEndDate = {!$Record.CloseDate} + 30.Validate that a Partner Contact's Mailing State matches the Account's Billing State.
{!$Record.MailingState} != {!varAccount.BillingState} → set flag.When Opportunity Stage = "Proposal/Price Quote" and Amount > $50,000, Description and Next Steps must be filled.
International contacts (Country != "United States") must have phone format +XX-XXX-XXX-XXXX.
formulaPhoneValid (boolean): REGEX({!$Record.Phone}, "^\\+[0-9]{2}-[0-9]{3}-[0-9]{3}-[0-9]{4}$")| Feature | Validation Rules | Before‑Save Flow | Apex Trigger |
|---|---|---|---|
| Complexity | Simple field checks | Moderate to complex | Unlimited |
| Cross‑object validation | Limited | Yes (Get Records) | Yes |
| Custom error messages | Yes | Yes (with flag+VR) | Yes |
| Performance | Fastest | Fast | Fastest |
| Maintenance | Easy | Moderate | Developer needed |
| Bulk operations | Excellent | Good | Excellent |
| Skill | Admin | Admin | Developer |
Business rules for Opportunity:
Flow structure:
START (Before Save Opportunity)
↓
DECISION: Should Validate? (stage changed or key fields)
↓ (Yes)
DECISION: Close Date > today? → else set flag
↓
GET Account → check customer status
↓
DECISION: Existing customer? (if yes, validate Amount)
↓
DECISION: Stage = Closed Won?
↓ (Yes)
DECISION: Competitor Analysis filled? → else flag
↓
GET Records: outstanding invoices (Account child)
↓
DECISION: Overdue invoices? → if yes, set flag
↓
END
1. Lightning Flow provides a powerful, declarative way to implement complex data validation logic without writing Apex. By leveraging Before‑Save Record‑Triggered Flows, admins can ensure data quality, enforce business rules, and provide immediate user feedback.
2. While the lack of a native "Add Error" action requires workarounds (like flag fields with validation rules), this approach still offers significant advantages in maintainability and visibility.
3. Start with simple validations, gradually build complexity, test thoroughly (especially bulk), and keep performance in mind.
4. With these techniques, you can maintain high data quality standards while empowering admins to own critical business logic.