You need to sign in to do that
Don't have an account?

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
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
All Answers
Below is the sample code to eliminate duplicates:
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
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
Please Try this code:-
Please mark as best answer if it helps you.
Thank You
Ajay Dubedi