Before Save flows and Validation Rules
To handle complex validations, for a long long time, we have been using Apex(mostly) and that’s only because there was no better substitute. However, with the introduction of Before save flows and the addition of sweet error handling and surfacing(last to last release I guess) you can get rid of lot of Apex(mostly, before create and update triggers related) in your org.
So let’s see what I’m talking about.
Use Case
Say, we want to prevent users from marking an Account as ‘Inactive'(via Active__c checkbox field) when there are open cases on Account.
What’s the idea?
The idea is to use before save flow and check a checkbox field which eventually leads to firing of a validation rule.
NOTE: I’ve used this pattern before with autolaunched flows but with the accurate displaying of error messages, this pattern becomes much more effective with before save flows.
Let’s see it in action
Step 1: Create a checkbox field, say OpenCasesValidation__c on Account object.
Step 2: Create a validation rule as follows:
Formula:
OpenCasesValidation__c
Error Field: Active__c
Error Message: Account with open cases cannot be marked as inactive.

Step 3: Create a flow that fires when Active__c field is updated to false and queries all the open cases and accordingly sets the OpenCasesValidation__c field. So let’s create the flow.
Substep 1: Create a new record triggered flow as shown:
Substep 2: Query open child cases using Get Records element.

Substep 3: Use Decision element to check if there are open cases or not.

Substep 4: Set the OpenCasesValidation__c field to true using Assignment element.

Save and activate your flow! And you’re all set.
Final Flow

Demo
NOTE: The surfacing of validation error only works with Before save flows. If you try to do the same with After save flows, you will get the old generic error saying following flow to execute and blah blah.
Hope you find it interesting. Catch you in the next one! ✌ Thank you for being an awesome reader! Subscribe to ForcePanda to receive all the latest updates straight to your inbox. 🙂

.png)




Comments