Saturday, 8 May 2021

How to save a record that’s been identified as a duplicate in apex

 Here we are going to learn how to save a record that's identified as duplicate with the help of DMLOptions.


Especially it helps to bypass duplicate rules in test class.


Eg:


Save an account record that’s been identified as a duplicate\


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!');



}

No comments:

Post a Comment