BigSolve home
    • AI AI agents that do the work, not the demo.
    • Analytics Reporting that runs on live data, not a weekly export.
    • Custom Development Custom builds for the workflow no product sold you.
    • Implementation Rollouts that survive contact with users.
    • Integration Integrations that move real money.
    • Case Studies We’d rather show you what changed.
  • Case Studies
  • About
  • Blog
Book a call (opens in a new tab)

Services

    • Case Studies
    • About
    • Careers
    • Blog
    • Contact
    Book a call (opens in a new tab)

    Blog

    Migrating Validation Rules from Apex to Flow

    Migrate Validation Rules from Apex to Flow’s Custom Error Element.

    Migrating Salesforce validation rules from Apex addError to Flow's Custom Error element

    In an attempt to demonstrate Flow's Custom Error element, let's walk through a hypothetical example of how this can be used in place of some legacy Apex code. Our goal is to create validation logic that prevents Users from storing more than 10 Contacts per Account.

    Apex

    Firstly, let's see how this could be done using Apex's addError() method:

    ContactTrigger

    trigger ContactTrigger on Contact (after insert, after update) {
    	// Validate Number of Contacts
    	ContactTriggerHandler.validateNumberOfContacts(Trigger.new);
    }

    ‍

    ContactTriggerHandler

    public class ContactTriggerHandler {
        
        // Method to validate the number of Contacts per Account
        public static void validateNumberOfContacts(List<Contact> contacts) {
            
            // Create a set to store Account IDs
            Set<Id> accountIds = new Set<Id>();
    
            // Populate the accountIds set with Account IDs from the Contact records
            for(Contact record : contacts) {
                accountIds.add(record.AccountId);
            }
    
            // Remove null values from the accountIds set (Contacts without an associated Account)
            accountIds.remove(null);
    
            // Create a map to store Account IDs and their corresponding Contact counts
            Map<Id, Integer> accountToContactCount = new Map<Id, Integer>();
    
            // Query to count Contacts per Account and filter Accounts with more than 10 Contacts
            for(AggregateResult aggResult : [
                SELECT AccountId, COUNT(Id) contactCount
                FROM Contact
                WHERE AccountId IN :accountIds
                GROUP BY AccountId
                HAVING COUNT(Id) > 10
            ]) {
                accountToContactCount.put((Id)aggResult.get('AccountId'), (Integer)aggResult.get('contactCount'));
            }
    
            // Loop through the Contacts and add an error message if the Account exceeds the limit
            for(Contact record : contacts) {
                if(accountToContactCount.containsKey(record.AccountId) && accountToContactCount.get(record.AccountId) > 10) {
                    record.addError('You cannot exceed 10 Contacts per Account.');
                }
            }
        }
        
    }

    ‍

    Flow

    And finally, we can make use of Flow's Custom Error element to fulfill the same set of requirements:

    The Contact After Create or Update record-triggered Flow in Salesforce, showing Get Records, Assignment, Decision, and Custom Error elements
    Record-Triggered Flow

    ‍

    Here are the Flow Elements in play:

    • Start Criteria
    • Get Contacts (Get Records)
    • Assign Count (Assignment)
    • Does Count Exceed 10? (Decision)
    • Error Message (Custom Error)

    ‍

    Ensure that the AccountId value is not null:

    Flow start criteria requiring AccountId to be not null before the validation runs
    Start Criteria

    ‍

    Query the Account's related Contacts:

    Get Records element querying Contacts where AccountId matches the triggering record
    Get Contacts (Get Records)

    ‍

    Assign the count of Contacts to a variable:

    Assignment element storing the Contact record count in a number variable
    Assign Count (Assignment)

    ‍

    Check to see if the count exceeds 10:

    Decision element checking whether the Contact count is greater than 10
    Does Count Exceed 10? (Decision)

    ‍

    If the count exceeds 10: generate an Error Message for the User:

    Custom Error element configured with the message You cannot exceed 10 Contacts per Account
    Error Message (Custom Error)

    ‍

    In an effort to reduce programmatic overhead, Salesforce Administrators can utilize Flow's Custom Error element in place of validations that used to require Apex Code.

    Keep reading.

    More from the engineers who do this for a living.

    BigSolve, Agentforce: Common Agentforce Implementation Mistakes
    Nov 19, 2026

    Common Agentforce Implementation Mistakes and How to Avoid Them

    BigSolve, Strategy: Why Salesforce Projects Fail
    Nov 16, 2026

    Why Salesforce Projects Fail: The Decisions Made Too Late

    BigSolve, Agentforce: 10 Agentforce Use Cases
    Nov 12, 2026

    10 Agentforce Use Cases and How to Choose Your First

    Let’s talk

    Bring us the mess.

    You’ll get a straight answer from someone who builds these systems, not a sales call.

    Book a call (opens in a new tab) Tell us what’s not working

    info@bigsolve.com(860) 245-1071

    BigSolve home

    The last Salesforce partner you’ll ever need.

    Salesforce Partner since 2022 (opens in a new tab)

    Services

    • AI
    • Analytics
    • Custom Development
    • Implementation
    • Integration

    Company

    • About
    • Case Studies
    • Careers
    • Blog
    • Contact

    Get in touch

    • info@bigsolve.com
    • (860) 245-1071
    • Book a call (opens in a new tab)

    LinkedIn (opens in a new tab) Reviews on Clutch (opens in a new tab)

    Keep Up With The Latest
    Join our subscriber list to get the latest news and special offers.
    Thank you! Your submission has been received!
    Oops! Something went wrong while submitting the form.
    bigsolve

    © 2026 BigSolve LLC. All rights reserved.

    Privacy Terms