I've currently enabled duplicate rules for the Contact object and specified the "Action on Create" and "Action on Edit" to "Block" incoming duplicate Contact records.

When testing with the example code provided in current documentation for the DmlOptions Class I'm still getting duplicate errors when trying to insert a duplicate contact record:

Contact duplicate_contact = new Contact(firstName='Bob', 

Database.DMLOptions dml = new Database.DMLOptions(); 
dml.DuplicateRuleHeader.allowSave = true;
dml.DuplicateRuleHeader.runAsCurrentUser = true;
Database.SaveResult sr = Database.insert(duplicate_contact, dml);
if (sr.isSuccess()) {
    System.debug('Duplicate contact has been inserted in Salesforce!');
else system.debug('>>> Error Inserting Duplicate: '+ sr.getErrors());

Error Received:

You're creating a duplicate record. We recommend you use an existing record instead.

Anyone know why specifying duplicateRuleHeader.allowSave = true would still produce this error?

  • @BrandOR: I just created standard duplicate rule on Contact and then used your exact code in anonymous code and it is working.
    – Mahmood
    Apr 4 '17 at 17:06
  • @Mahmood - Try setting the action to Block on the duplicate rule and then specifying the allowSave header. If it's set to block this will cause duplicate inserts to fail even when allowSave = true
    – Brand0R
    Apr 4 '17 at 18:13
  • Thats what I did, set the duplicate rule action to block, and it was saved.
    – Mahmood
    Apr 4 '17 at 18:34

This is actually working as designed. The DuplicateRuleHeader.AllowSave option is really allowing for bypassing of the duplicate rule alert. If the duplicate rule specifies a blocking action this will block any and all duplicates found even when specifying AllowSave = true.

There has been a documentation bug filed for this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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