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?