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
sheila srivatsavsheila srivatsav 

unable to remove duplicate lastnames from contact

I have 10000 records  in contact object and 5000 of them are duplicate contact lastname.

My requirement is to eliminate the duplicates lastnames in contact object.

I have soql script which unfortunately doesnt work.

List<Contact> conList=[select ID,LastName from contact LIMIT 50000];   
Map<String,contact> conMap=new Map<String,Contact>();

for(Contact c:conList)
{
   conMap.put(c.LastName,c);
}

//at this point the map should merge the duplicates.  Please correct my understanding

Now datbase.update(conMap.values());

But once update statement is executed, the duplicate records still exists. Only the map has the updated records.

Please let me know if this approach is right....

thanks
sheila




 
Best Answer chosen by sheila srivatsav
Raj VakatiRaj Vakati
Try some think like below
 
List<Contact> conList=[select ID,LastName from contact LIMIT 50000];   
Map<String,contact> conMap=new Map<String,Contact>();
	List<contact> duplicatelist = new List<contact>();

for(Contact c:conList)
{
		  if(! conMap.containsKey(c.LastName)){

            conMap.put(c.LastName,c);
		  }else{
			 duplicatelist.add(c) ; 
		  }
}


delete duplicatelist ;

 

All Answers

Raj VakatiRaj Vakati
Try some think like below
 
List<Contact> conList=[select ID,LastName from contact LIMIT 50000];   
Map<String,contact> conMap=new Map<String,Contact>();
	List<contact> duplicatelist = new List<contact>();

for(Contact c:conList)
{
		  if(! conMap.containsKey(c.LastName)){

            conMap.put(c.LastName,c);
		  }else{
			 duplicatelist.add(c) ; 
		  }
}


delete duplicatelist ;

 
This was selected as the best answer
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sheila,

Below is the sample code to eliminate duplicates:
 
public static List<String> getDistinctLastnames(List<String> duplicates){   
    List<String> distinctLastnames = new List<String>();    
    for(String lastname: duplicates){        
        Boolean found = false;       
        for(Integer i=0; i< distinctLastnames.size(); i++){            
            if(lastname.equalsIgnoreCase(distinctLastnames[i])){ //Check if current lastname has been added yet                
                found=true;                
                break;                
            }            
        }        
        if(!found)            
            distinctLastnames.add(lastname);        
    }   
    return distinctLastnames;   
}

Please refer this link for better understanding:
https://th3silverlining.com/2009/06/28/soql-distinct-keyword/


I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
PreyankaPreyanka
Hello Sheila,

Are you want to delete the duplicate record or want to merge the duplicate record. If you want to merge duplicate record then you can do it via User Interface refer to https://help.salesforce.com/articleView?id=contacts_merge_classic.htm&type=5 or you can do it via apex also, but at a time only 3 record can be merged. You can use Database.merge() statement for this. Please refer to below link for merge using apex.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_merge.htm
https://developer.salesforce.com/forums/?id=906F0000000AvIoIAK 

Thanks
Preyanka
Ajay K DubediAjay K Dubedi
Hi Sheila,

Please Try this code:-
 
List<Contact> allContact = [SELECT LastName FROM Contact];
Set<String> ContactNames = new Set<String>();
List<Contact> ContactToDelete = new List<Contact>();
for (Contact a : allContact) {
    if (ContactNames.contains(a.LastName)) {
        ContactToDelete.add(a);
    }
    else {
        ContactNames.add(a.LastName);
    }
}
delete ContactToDelete;

Please mark as best answer if it helps you.

Thank You
Ajay Dubedi