How to bypass duplicate rules via Apex in Salesforce?

How to bypass duplicate rules via Apex in Salesforce?

DuplicateRuleHeader in Apex

ยท

3 min read

In this article, we will learn how to save duplicate records using Apex if a duplicate rule is already configured in Salesforce.

I am assuming that you already have a duplicate rule configured in Salesforce. In this article, we will be using the example of Account SObject.

What are duplicate Rules?

According to Salesforce help article A duplicate rule defines what happens when a user views a record with duplicates or starts creating a duplicate record*.*

While configuring duplicate Rules you might have seen options of Action on Edit/Create which either allows or blocks duplicates.

You can see the below image for reference.

As you can see I have created a duplicate rule on the account object which blocks whenever a duplicate record is created/updated.

Create an Account Record Using Apex

As you can see below image I have already created an Account record from UI with Account Name Shubham Lashkan.

Now let's try creating one more account record with the same information using Apex. Copy and execute the below code from the Anonymous window.

Account acc = new Account();
acc.Name = 'Shubham Lashkan';
acc.phone = '74102589630';
acc.website = 'https://www.shubhamlashkan.com';
acc.type = 'Technology Partner';
acc.Industry = 'Technology';
insert acc;

Once you execute the code you will see a DMLException error message like the below image.

Because of the duplicate rule being configured for the account object insertion of the duplicate records is not allowed and a DMLException is thrown.

This duplicate rule can be bypassed by setting the DuplicateRuleHeader property.

What is DuplicateRuleHeader?

This property determines whether a record that is identified as a duplicate can be saved or not.

We have to set the AllowSave option of the DuplicateRuleHeader property while performing insertion or updation using apex.

AllowSave is of type boolean which means it accepts either true or false.

Let's use it with the code which we already executed above and allow saving duplicate records.

Using the AllowSave option in Apex

Execute the following code from the anonymous window and you will notice that this time no DMLException is thrown.

Account acc = new Account();
acc.Name = 'Shubham Lashkan';
acc.phone = '7410258963';
acc.website = 'https://www.shubhamlashkan.com';
acc.type = 'Technology Partner';
acc.Industry = 'Technology';
Database.DMLOptions dml = new Database.DMLOptions(); 
dml.DuplicateRuleHeader.AllowSave = true;
Database.SaveResult sr = Database.insert(acc, dml);
if (sr.isSuccess()) {
    System.debug('Duplicate account has been inserted in Salesforce!');
}

DuplicateRuleHeader is a property of DMLOptions that comes under Database Namespace. And AllowSave is a property of DuplicateRuleHeader.

We have set AllowSave to true to allow the saving of duplicate records.

And instead of DML operation, we have used Database methods for performing insertion.

Open the debug logs and you will see the following.

Also, open the Accounts tab and you will see both records.

Conclusion

Duplicate Rules configured in Salesforce org can be bypassed by setting the AllowSave option of DuplicateRuleHeader property to true while performing insertion or updation operation.

The only problem is that this option can only be set by performing database operations using Database methods.

I hope I am able to explain to you how to bypass duplicate rules using apex in Salesforce.

I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here. Let me also know which topic I should write about next.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!

ย