+ Start a Discussion
Uday KUday K 

Removing Duplicate rows Using Apex code or excel

Hi,

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
00QU0000006idQI3/26/2008ErinJeffersReadingPA
00QU0000006igC03/25/2008ErinJeffersReadingPA

 

Thanks,
Uday

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

Snippet

 

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

{

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

                ListOfLeadIdsToBeDeleted.add(Lead.Id)

     ELSE

                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.

 

Thanks,

Praful G.

veerna551.3942206611176638E12veerna551.3942206611176638E12
good can u provide the full code for this.
nmhnmh
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.

Regards,
Praful Gadge.