0

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?

5
  • Why are you using apex batch ? Try running your code(Logic) in Anonymous Window and check if you are getting expected output Commented Jun 9, 2017 at 5:55
  • so that it will update contacts daily Commented Jun 9, 2017 at 5:56
  • So your requirement is to check duplicate out of latest 10 records only ? Commented Jun 9, 2017 at 5:58
  • Why not restrict the insert/update of such contacts using a trigger validation? Or in a configurable way as well. If your purpose is to allow insert but mark as duplicate, you can do that using the trigger as well. Commented Jun 9, 2017 at 6:02
  • @Muzammil Bajaria I'm testing with 10 contacts only, I'll remove the limit after testing Commented Jun 9, 2017 at 6:49

1 Answer 1

1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .