+ Start a Discussion
Uday KUday K 

Removing Duplicate rows Using Apex code or excel


I have 4 fields First Name, Last Name, Destination City, Destination State. These 4 columns have some duplicate values. I want to remove those Duplicate rows and these duplicate rows have different "Created Date".   have to delete only old dated rows. can someone help me. These records are already in database.

Lead IDCreate DateFirst NameLast NameDestination CityDestination State
00QU0000006ideK3/23/2008danramirez TX



Praful GadgePraful Gadge

Hi Uday,


It is possible through batch apex which you can schedule daily or weekly. Which will be your one time solution to remove dupes.


You can follow these steps:

  1. Create an apex class with a scheduler (I hope you know the scheduled batch apex)
  2. In apex class
  • Fetch and iterate Leads order by Created Date(Descending)
  • Maintain a Set<String> in which we are adding FirstName_Last Name_Destination City_Destination State
  • In each iteration before adding into string check if Set contains a string and if Yes add to a Set<Id> of Lead to delete leads after iteration



for( Lead : SOQL to fetch Leads order by Created Date )


     IF( setOfStringOfFN_LN_DC_DS.contains( Lead.FN_Lead.LN_Lead.DC_Lead.DS ) )



                setOfStringOfFN_LN_DC_DS.add( Lead.FN_Lead.LN_Lead.DC_Lead.DS )



delete ListOfLeadIdsToBeDeleted;


It will resolve your problem for sure, Let me know if you have any doubts,


If this post is your solution, kindly mark this as the solution to the post so that others may benefit.



Praful G.

good can u provide the full code for this.
Worked for me. Thank you
Praful GadgePraful Gadge
Hi Uday,

If the above post is your solution, kindly mark this as the solution so that others may benefit.

Praful Gadge.