14

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.

2
  • What is this 'standard' duplicate check?
    – user39399
    Commented Jun 4, 2015 at 13:02
  • The data.com duplicate management Commented Jun 4, 2015 at 13:02

2 Answers 2

30

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!'); 
}
4
  • Amazing! It works. Would you mind to share the code from the site in your answer? If one day the link will be down, your answer wont be helpful for others. Thank you really much. Commented Jun 4, 2015 at 14:42
  • Glad that it helped. I've updated the answer with the sample code.
    – user39399
    Commented Jun 4, 2015 at 15:43
  • Tried the similar code in trigger and it does not work. Does anybody know about any limitations in trigger context? Commented Sep 1, 2016 at 14:57
  • Do you add this apex code into the apex class in scope or do you create a separate apex class?
    – andrew
    Commented Nov 7, 2022 at 17:43
8

Apparently the AllowSave only works if the duplicate rule is an Alert rule, not a Block rule.

When you try to save a record that’s identified as a duplicate record by a duplicate rule, you’ll receive a duplicate error. If the duplicate rule contains the Allow action, an attempt will be made to bypass the error.

Documentation

2
  • @ Ratan the above isn't what I'd consider a link only answer. He's provided a link to the reference documentation where the user can find further information about his answer.
    – crmprogdev
    Commented Jan 28, 2016 at 17:04
  • Didn't have enough cred to add a comment. Commented Feb 3, 2016 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .