Here is my requirement and I am confused on how to solve this. So please help me.


We have set of leads, the count could be any where from 50 to 1000 leads. Client wants to know for each lead, If there are already matching leads in the system based on the given conditions like same first name,last name,email,phone. In our system we have leads of close to 200,000.

Here is the solution that I am thinking:

global class findDuplicateLeadsBatch implements Database.Batchable<sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('SELECT Id,FirstName,LastName,Email,Phone FROM Lead WHERE CampaignId=\'1223234334\'');

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     Lead leadObj = (lead)scope;
     Custom_Obj__c custObj = new Custom_Obj__c();

     //Here how do I find the duplicate leads matching the current lead in scope based on the first name,lastname, email and phone.
     custObj.LeadId__c = leadObj.Id;
     custObj.duplicateExists__c = TRUE;
     custObj.duplicateLeadId__c = ''; //If I can find successfully the duplicate leads, I want to store in this variable the Ids.

     insert custObj;

   global void finish(Database.BatchableContext BC){
    //code to send email that batch job completed.

and I am going to start the batch like this:

Database.executeBatch(new findDuplicateLeadsBatch(), 1);

My questions are :

  1. Is this approach valid for the requirement I am working on or is there a better approach?
  2. If yes, What query or How do I find the duplicate leads matching the current lead in scope based on the given fields.Should I use SOSL?

Thanks in Advance!

  • Hi @javanoob, in salesforce itself we do have a option to find duplicate lead. you can yous that.
    – Anu
    Aug 14, 2015 at 4:45
  • I have a list of 500 leads for which I want to find duplicate leads. if i am wrong, you are referring to the find duplicates button on the UI right?
    – javanoob
    Aug 14, 2015 at 4:47
  • @Anu, Do you know the method/API name which gets called when we click on the Find Duplicates button on the Lead Detail page?
    – javanoob
    Aug 14, 2015 at 4:50


You must log in to answer this question.

Browse other questions tagged .