Activity Forums Salesforce® Discussions How to delete duplicate records in Salesforce? Reply To: How to delete duplicate records in Salesforce?

  • Archit

    Member
    January 10, 2018 at 10:27 am

    Hello Ankit,

    If you consider duplicate's in terms of name or any particular field of any object might be the below code would be helpful to get your answer. I tried below code to remove duplicate name records of Accounts object.

    List<Account> fullList = [Select Id, name From Account limit 200];
    Set<String> orderObj = new Set<String>(); 
    List<Account> dupList = new List<Account>();
    
    for(Account a : fullList)
    {
     if(orderObj.contains(a.name))
     {
     dupList.add(a);
     }
     else
     {
     orderObj.add(a.name);
     }
    }
    
    delete dupList;
    

    Hope the answer would be helpful!!