Let us first understand the Validation Rule and its characteristics.
A Validation Rule allows a System Administrator to define custom logic and error messages to ensure data integrity. The Rule can contain a formula, or an expression, that evaluates the data in one or more fields and returns a value of True or False. For instance, the Rule includes an error message that displays when it returns a value of True — indicating that inaccurate data is being entered. Remember, a Validation rule only fires when a record is created or edited.
In this article, I talked about many hidden facts that are not well known, about the Validation Rule. For example, I showed how one can use a Validation Rule, to manage requirements, using Custom Metadata types.
While writing the Formula for a Validation Rule one can only reference fields from the parent record. For example, when writing a Validation Rule on Opportunity one can reference fields from Account but not Quote (which is a child of Opportunity) – as shown in the following screenshot:
Let us start with a business use case.
After reading this blog, you’ll be able to:
- Understand what is a before-save Flow.
- Understand when to use before-save vs after-save Flow.
- How to use before-save Flow to write a Validation Rule.
Business Use case
Edward Backhouse is a System Administrator at GurukulOnCloud. Edward received the following requirement from his manager – make sure an Account doesn’t have any open Opportunity if its ‘Out_of_busienss__c’ field value is updated to True.
So, the requirement is, write a Validation Rule such that, if the Account has an open Opportunity then, display the following error message:
You can’t deactivate an Account that has an open Opportunity. First, update the Opportunity status, to ‘Closed Won’ or to ‘Closed Lost’, and then deactivate the Account.
What is Before Save Flow?
Use before-save updates Flow to accomplish the same goal as Process Builder (After-save Flow) – but, much faster because, each record does not get saved to the database again. Avoiding that extra save procedure means skipping another round of Assignment rules, Auto-response rules, Workflow rules, and other customizations that take time to execute.
Use a Before-save Flow on the following use cases:
- Update fields on new or changed records
- Trigger a Validation rule (this is a custom solution not available out-of-the-box)
Use an After-save Flow on the following use cases:
- Create or update related records.
- Access fields like the Last Modified Date field or the new Record’s ID, only populate after the record is saved.
- For example, sending Email alerts.
- Perform actions other than updating the record that launches the Flow.
Automation Champion Approach (I-do):
Usually, with Salesforce, multiple approaches are available to solve a business requirement. Choose the ones that are simple, straight forward, and consume fewer resources.