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
Vaibhav BabrekarVaibhav Babrekar 

I have the following requirement can anyone help me getting solution for this?

Removal of Duplicate Leads
1.During the Campaigning, it might happen that representative creates duplicate leads in an org.
2.So admin want to build a process which will run every 3 hours/day & remove the duplicate leads from the org.
3.The criteria to find the duplicate records should be configurable.Ex. If there are two leads in a system with same Email address,then keep the first lead entry & remove all the other leads.
4.So the field like Email, Name which will be deciding the uniqueness should be configurable.
PriyaPriya (Salesforce Developers) 
Hey Vaibhav,

You need to write the batch class :-
Batch Class ----->

global class removeDuplicateRecords implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'select Email,Name,Company,Description from Lead where Email != Null';
        return Database.getQueryLocator(query); 
    }
    
    global void execute(Database.BatchableContext BC, List<Lead> scope)
    { 
        Map<String,Lead> EmailLead = new Map<String,Lead>();
        List<Lead> LeadInfo = new List<Lead>();
        List<Lead> duplicatelist = new List<Lead>();
        for(Lead objLead : scope)
        {
            if(!EmailLead.containsKey(objLead.Email))
            {
                EmailLead.put(objLead.Email,objLead);
            }
            else
            {
                duplicatelist.add(objLead);
            }
        }
        for(Lead objLead1 : duplicatelist)
        {
            Lead lead_new = EmailLead.get(objLead1.Email);
            if(lead_new.Description != Null)
            {
                lead_new.Description = lead_new.Description + (';'+ objLead1.Name +','+ objLead1.Company);
                LeadInfo.add(lead_new);
            }
            else
            {
                lead_new.Description = (objLead1.Name +','+ objLead1.Company);
                LeadInfo.add(lead_new);
            }
        }
        if(LeadInfo.size() > 0)
        {
            update LeadInfo;
        }
        if(duplicatelist.size() > 0)
        {
            delete duplicatelist;
        }
    }
    global void finish(Database.BatchableContext BC)
    {
        
    }
}



Refer this link :- https://developer.salesforce.com/forums/?id=9062I000000Xmt3QAC
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan