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
Shanky77Shanky77 

Deletion of dulicate records from database

can any body tell me how to delete the duplicate records from the database at one stroke.

 

I have collected the all duplicate records in a list and when I am trying to delete the list using

"delete list" . Instead of cleaning up it is again n again throwing an exception called "mulitile DML statements with same ids".

BritishBoyinDCBritishBoyinDC

Sounds like one or more records you are trying to delete is getting into the list more than once - Lists don't enforce uniqueness.

 

Sets do enforce uniqueness -  I would try add all the records from the list into a set, and compare the size of the set to the list - if the set is smaller, you'll need to find a way to enforce uniqueness in the list before executing a delete statement

sfdcfoxsfdcfox

You can add all of the records from the list into a Map. Maps are definitely unique as well. As a bonus, you don't have to work out anything special at all. Actually, you can do it even easier than that:

 

Set<id> idsToDelete = new set<id>(); for(sobject s:listtodelete) idstodelete.add(s.get('id')); list<id> finalListToDelete = new list<id>(); finalListToDelete.addAll(s); delete finalListToDelete;