[SalesForce] DuplicateRuleHeader.AllowSave = True not working

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', 
                                        lastName='Duplicate',
                                        email='test@test.org');

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?

Best Answer

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.

Related Topic