Disable duplicate check for apex class?

Is there a way to disable the duplicate check for an apex class?

I have an insert of contacts, where I have a custom duplicate check. Now I would like to disable the standard duplicate check for only that apex class.

Answer

I think you can bypass duplicate check with DMLOptions.DuplicateRuleHeader Class: documentation

Sample:

Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.AllowSave = 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!'); 
}

Attribution
Source : Link , Question Author : Frederik Witte , Answer Author : bananka

Leave a Comment