[SalesForce] How to get duplicate contacts using batch apex

I'm trying to get duplicate contacts using batch apex, for that, I want to check a field lender__c on contact if(lander__c == 'loanDepot') then I want to check their email and accounts if both are same then I want to mark the as duplicate

for my code is as follows

global class DuplicateContactsBatch implements Database.Batchable<sObject>, Database.Stateful{

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('Select id, Name, AccountId, Lender__c, ownerId, Email from contact order by CreatedDate desc limit 10'
        );
    }

    global void execute(Database.BatchableContext bc, List<Contact> scope){
        List<Contact> contactslist = new List<Contact>();


        map<String,List<Contact>> Contactmap = new map<String,List<Contact>>();

        for(Contact objcon : scope)
        {
            if(objcon.Lender__c == 'loanDepot' )
            {
                if(Contactmap.containsKey(objcon.email+objcon.AccountId))
                {
                    Contactmap.get(objcon.email+objcon.AccountId).add(objcon);
                    System.debug('Get Contactmap>>'+Contactmap);
                }
                else
                {
                    List<Contact> listcon = new List<Contact>();
                    listcon.add(objcon);
                    Contactmap.put(objcon.email+objcon.AccountId,listcon);
                }
                System.debug('Contactmap-->'+Contactmap);
            }   
        }
    }
    global void finish(Database.BatchableContext bc){
    }   
}

Here I'm able to add all the duplicate contacts in the map. If suppose their are 3 contacts whose email and account id is same I want to mark 2 contacts as duplicate which are recent one and one as original, how can I do this.? and for this is it ok to use batch apex?

Best Answer

First, you can locate duplicate records without using programmatic solutions. All you need do is implement Salesforce's native duplicate checking rules that will detect existing duplicate contacts and prevent new ones from being created. The cardinal rule for developers is to never write code when point & click solutions are available to do the job for you.

That having been said, your query pointer isn't written properly for batch apex. Using your logic, you'll want to return Account Ids. Then in your execute method, query for contacts that have the same Contact.Lender__c = 'loanDepot", the same Contact.email and Contact.AccountId (if that's your matching criteria). That's a direct solution.

Your code as written doesn't appear to create the map before you sort thru it. If you found any matches, you'd want to add ALL of them to a new map of <accountId, contactId> and a map of <ContactId, CreatedDate> that you could later sort by created date of the contact to determine which records to keep.

Related Topic