Flows And Validation Rules Prior To Saving

For a considerable duration, we have primarily relied on Apex to manage intricate validations due to the absence of a more suitable alternative. However, with the advent of Before Save flows and the incorporation of robust error handling and surfacing (which I believe was introduced in the release before the last), you now have the opportunity to reduce reliance on a significant amount of Apex code, particularly concerning before-create and before-update triggers in your org.

Now, let’s delve into what I’m referring to.

Scenario

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 concept?

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

NOTE: The presentation of validation errors is effective only with Before Save flows. If attempted with After Save flows, you’ll encounter the traditional generic error prompting the execution of the following flow and so on.

Hope you find this intriguing. See you in the next one! A big thank you for being an excellent reader! Subscribe to ForcePanda to get all the latest updates directly in your inbox.