function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sandy sfdcsandy sfdc 

How to get possibility duplicate records in Apex class or Api

Hi,
 Duplicate alert

As shown in the image i need to get the possible duplicate records when i am inserting record from apex class or Api.

when i am executing from apex class with the following code
Account ll =  new lead();
ll.Name = 'Burlington Textiles Corp of America';
insert ll;

it showing error without possibilities like this
User-added image

Please help on this.
Best Answer chosen by sandy sfdc
RAM AnisettiRAM Anisetti
Hi Sandy,

Can you try this below code in your apex class or custom service class(REST or SOAP)
 
Account a=new Account();
a.name='Burlington Textiles Corp of America';

Database.SaveResult res=DataBase.insert(a,false);

if (res.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + res.getId());
    }
    else {
        
         Datacloud.DuplicateResult duplicateResult;
         List<Datacloud.MatchRecord> matchRecords;
         List<Id> duplicateIds = new List<id>();
        // Operation failed, so get all errors                
        for(Database.Error duplicateError : res.getErrors()) {
            
        duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult();
        System.debug('The following error has occurred.--duplicateResult--'+duplicateResult);                    
            
        }
        
        for(Datacloud.MatchResult duplicateMatchResult : duplicateResult.getMatchResults()) {
        matchRecords = duplicateMatchResult.getMatchRecords();
        System.debug('The following error has occurred.--matchRecords--'+matchRecords);  
      }
        System.debug('total.--matchRecords--'+matchRecords);  
        for(Datacloud.MatchRecord duplicateMatchRecord : matchRecords) {
        duplicateIds.add(duplicateMatchRecord.getRecord().Id);
            System.debug('duplicateIds--'+duplicateIds);
        
       }
         System.debug('Total duplicateIds--'+duplicateIds);
        
        
    }

 

All Answers

Adrian ChiewAdrian Chiew
It looks like you have a validation rule in place. You would not usually have an error for creating an Account with the same Name as an existing record.

What you will want to do is first to do a search for existing records that match for the Name of the new Account you're trying to create. Exactly what you put in your search would depend on your validation rule. You may also want to use some criteria to return results that although aren't an exact match, have a very similar name.

For example you might have something like.

Account ll =  new lead();
ll.Name = 'Burlington Textiles Corp of America';

Then do a search so:

List<Account> possibleDuplicateAccounts = [Select Id, Name from Account WHERE Name like ('%' + ll.Name + '%')];
sandy sfdcsandy sfdc
Thanks for reply,
But i am looking to get the Possibilities from salesforce Data.com - Duplicate Management.
please refer the below Url's 
https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_prevention_map_of_concepts.htm&language=en_US
http://blog.sonomapartners.com/2015/01/duplicate-management-and-salesforce-spring-15.html
RAM AnisettiRAM Anisetti
Hi Sandy,

Can you try this below code in your apex class or custom service class(REST or SOAP)
 
Account a=new Account();
a.name='Burlington Textiles Corp of America';

Database.SaveResult res=DataBase.insert(a,false);

if (res.isSuccess()) {
        // Operation was successful, so get the ID of the record that was processed
        System.debug('Successfully inserted account. Account ID: ' + res.getId());
    }
    else {
        
         Datacloud.DuplicateResult duplicateResult;
         List<Datacloud.MatchRecord> matchRecords;
         List<Id> duplicateIds = new List<id>();
        // Operation failed, so get all errors                
        for(Database.Error duplicateError : res.getErrors()) {
            
        duplicateResult = ((Database.DuplicateError)duplicateError).getDuplicateResult();
        System.debug('The following error has occurred.--duplicateResult--'+duplicateResult);                    
            
        }
        
        for(Datacloud.MatchResult duplicateMatchResult : duplicateResult.getMatchResults()) {
        matchRecords = duplicateMatchResult.getMatchRecords();
        System.debug('The following error has occurred.--matchRecords--'+matchRecords);  
      }
        System.debug('total.--matchRecords--'+matchRecords);  
        for(Datacloud.MatchRecord duplicateMatchRecord : matchRecords) {
        duplicateIds.add(duplicateMatchRecord.getRecord().Id);
            System.debug('duplicateIds--'+duplicateIds);
        
       }
         System.debug('Total duplicateIds--'+duplicateIds);
        
        
    }

 
This was selected as the best answer
sandy sfdcsandy sfdc
Thanks Ram 
It is working...
shoba shobashoba shoba
Hi Sandy

Am also facing the same problem for update proces. I overrride the edit page of account uisng an apex class. I tried the RAM Anisetti code. Its throwing an error like "Invalid conversion from runtime type Database.Error to Database.DuplicateError
Error is in expression '{!CustomSave}' in component <apex:commandButton> in page customsavepage: Class.CustomControllerSaveofAccount.CustomSave: line 34, column 1". If you faced the same issue can you please help  me how to rectify this.
 
sandy sfdcsandy sfdc
hi shoba 
Could u plz provide the sample code
hanumanth rao Naiduhanumanth rao Naidu
Hi Sandy ,

I am also facing the same situation , could you please send me the code to my email id 
hanu.akki@gmail.com   
----its very urgent requirement