I have activated the standard Duplicate Management Rule on Contact to prevent duplicate records.

When I try to create duplicate Contact records form SalsesForce side, it shows me a warning message and allows me to ignores the alert and create a duplicate record.

But when I try to create a duplicate record in Apex, it gives me an exception.

How can I surpass/ignore such alerts and create duplicate records in Apex?

Kindly help me out.


User the DuplicateRuleHeader of the DmlOptions class. The example in the documentation looks like this:

Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
dml.DuplicateRuleHeader.runAsCurrentUser = true; 
Account duplicateAccount = new Account(Name='dupe');
Database.SaveResult sr = Database.insert(duplicateAccount, dml); 
if (sr.isSuccess()) {   
  System.debug('Duplicate account has been inserted in Salesforce!'); 

Not the answer you're looking for? Browse other questions tagged or ask your own question.