You need to sign in to do that
Don't have an account?
tcsells
Duplicate IDs in DML list
I have a trigger that updates records in a list. The list will potentially have duplicates and will cause the DML operation to fail with the “System.ListException: Duplicate id in list” error. I thought I could solve it by populating a set, then converting the set to a list (see code), but that didn’t work either.
if (!commitupdates.IsEmpty()){
List<WV_Commitments__c> commitupdatelist = new list<WV_Commitments__c>(); commitupdatelist.addall(commitupdates); update commitupdatelist; }
Is there a reliable way to ensure that no duplicate objects are added to a list or set for a DML operation? Thanks in advance for any suggestions.
Todd
Hi.
I had the same the problem and using a set worked for me. This is my code:
Let me know if that worked
Hi Todd,
Another way to populate your list is to put them into a map, and then get the list by calling the values() method. That will ensure you won't have a duplicate IDs.
-Hengky-
Thanks for the solutions. I discovered that my problem was that even though the object ID was a duplicate, the actual object was not because it had different data in the fields.
My solution was to track the IDs in a set, and only add the object if I successfully added the ID to the set. I don’t know if this is the best practice, but it works.
Thanks again for the feedback.
-Todd