Duplicate ID in list causes whole UPDATE to fail although allOrNone is set to false

523    Asked by alexDuncan in AWS , Asked on Apr 16, 2021

A duplicate ID in my list of SObjects "businessPartnerListToUpdate"

Database.SaveResult[] saveResultList = Database.update(businessPartnerListToUpdate, false);

causes an Exception to be thrown: System.ListException: Duplicate id in list: 0011r00001mIDEAAA4 I was having the assumption that since I set allOrNone to false, the rest of the records will still succeed, but this is not the case. Why is this so?

Answered by David EDWARDS

It's true that allOrNone set to false will result in inserting or updating those records that don't have errors associated with them. However, in this case, the exception that's raised has to do with the whole DML operation, not with a single record. I think of this error as Salesforce indicating that there is an internal inconsistency in what it was asked to do, and hence it performs no work at all. The typical remedy for this error is to accumulate records to be updated in a Map, and then do Database.update(myMap.values(), false); This will ensure that each Id is present only once.



Your Answer

Interviews

Parent Categories