+ Start a Discussion
shoba shobashoba shoba 

How to replicate the duplicate management rule in apex class

In Account object, therre is a duplicate management rule for account name. We override the edit page of account. The problem is  when the user try to create a duplicate record it throwing the error like "Update failed. First exception on row 0 with id 001Q0000010c9osIAA; first error: DUPLICATES_DETECTED, You may be creating a duplicate record. If so, we recommend you use the existing record instead.: []
Error is in expression '{!CustomSave}' in component <apex:commandButton> in page customsavepage: Class.CustomControllerSaveofAccount.CustomSave: line 21, column 1".
But i wan to show like this
User-added image
Can anyone help me out to solve this issue.
Shailendra Singh ParmarShailendra Singh Parmar
Hi Shoba,
You could show those custom messages by handling errors messages and then building custom logic around your save result. Something like below 
Database.SaveResult result = Database.insert(item);
if(result.isSussces == false) {
      for(Database.Error duplicateError : res.getErrors()) {
              if(String.valueOf(duplicateError.getStatusCode()).equals('DUPLICATES_DETECTED')) {
                 Datacloud.DuplicateResult duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult();
                 for(Datacloud.MatchResult duplicateMatchResult : duplicateResult.getMatchResults()) {
                     List<Datacloud.MatchRecord> matchRecords = duplicateMatchResult.getMatchRecords();
                     // Now you have list of all matching records. Explore Datacloud.MatchRecord> and you have option to collect all those records and show according to you requirment

Look for Datacloud.DuplicateResult  and Datacloud.MatchResult salesforce.com documentation and you can get what you needed.