Migrate Validation Rules from Apex 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.
We'll compare the following solutions in solving this requirement:
Firstly, let's see how this could be done using Apex's addError() method:
And finally, we can make use of Flow's Custom Error element to fulfill the same set of requirements:
Here are the Flow Elements in play:
Get Contacts (Get Records)
Assign Count (Assignment)
Does Count Exceed 10? (Decision)
Error Message (Custom Error)
Ensure that the AccountId value is not null:
Query the Account's related Contacts:
Assign the count of Contacts to a variable:
Check to see if the count exceeds 10:
If the count exceeds 10: generate an Error Message for the User:
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.