4

I a have a problem in my code, i try to merge two lists into a Sobject list in order to optimize the DML operation, but i get this error during run time:

Collection store exception adding all List<Opportunity> to List<Account>

here is my code:

List<sObject> listSobjects = (List<Sobject>) listAccounts;
List<sObject> listSobjectOpportunity = (List<sObject>) opportunityList;
listSobjects.addAll(listSobjectOpportunity);
Database.SaveResult[] tt = Database.update(listSobjects, false);

Any ideas how to make it work.

1 Answer 1

12

Instantiate a new generic list and it won't carry over any type information. Simply casting to List<SObject> doesn't remove the fact it was originally a List<Account>.

List<SObject> records = new List<SObject>();
records.addAll(accounts);
records.addAll(opportunities);
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .