A common request in Salesforce is the ability to prevent duplicate records from being created. The most common way to prevent record creation or updates in Salesforce is through a validation rule. But can a validation rule prevent duplicates? The short answer: no. A standard validation rule can only use specific record data, so it can’t find potential duplicate records. But pairing a validation rule with a before save flow can!
Salesforce’s order of operations gives us an opportunity to execute a flow before validation rules fire. This means we can perform checks, and trigger a validation rule before the record is created/edited.
Let’s work through a sample scenario to see how this works.
You are the Salesforce admin at Universal Machinery, a company that manufactures massive machines used in factories around the world. When a machine is sold to your clients, they are also provided with a service plan and warranty. The machines are tracked in your system as Assets.
If a machine experiences issues, there are potentially several people who could report this issue. Someone could call the support line, someone could directly contact a customer support manager, or they could contact a sales representative. In order to help your team focus on their cases, you want to make sure that a new case isn’t created by each of these users. If there is already an open case for a given asset, you want to prevent that duplicate from being created, and notify the user.
To meet these requirements, we need to create three elements. First, we need a checkbox field for validation. In our example, we’ll call it Prevent Save. This is simple checkbox field, with the default value of unchecked. We’ll check this field in our flow.
Next, we need to create the validation rule that will prevent a user from creating the duplicate record. This rule only needs to look at our Prevent Save checkbox. Since validation rules fire when the criteria is true, we only have to add the API name of the field. If it is checked, this rule prevents the record from being saved. Otherwise, it progresses as normal.
Finally, we can create our flow. This is a before save flow, that exists on the case object. This flow has four elements.
First, we need to check if there is an asset connected to the case. If ‘Asset’ was a required field on cases, we could skip this step.
Next, we need to search for any records that would be considered a duplicate. In our example, that means finding records that meet 2 criteria. First, that the Asset.Id is the same as the $Record.Id. Second, that the Status does not equal ‘Closed’. We only care about active cases in our scenario.
After we check for records, we’ll set a decision point. If a matching record was found, we’ll check our ‘Prevent Save’ checkbox. Otherwise, we’ll let the record proceed.
Testing as a user, we can verify that if there is an open case related to the same asset, we are notified of the error. A duplicate record isn’t created, helping our team collaborate.
While a standard Salesforce validation rule can’t prevent duplicates, utilizing a before save flow can. This same idea can be used for any object, and many different scenarios. Many of my clients have found this extremely useful.