I have some abstracted code, which at the end of some process will create a homogeneous list of SObjects.
It could be a list of Accounts, it could be a list of contacts, it could be a list of some custom SObject. It doesn't matter, every record in the list will be an instance of the same type of SObject.
upsert resultSObjectList
I can't simply do an because it results in:
System.TypeException: DML on generic List only allowed for insert, update or delete
If I send the list back to whatever method called my abstraction and then try to cast it back into whatever type it should be, e.g.:
List<Account> accountList = (List<Account>) resultSObjectList;
upsert accountList;
It still results in:
System.TypeException: DML on generic List only allowed for insert, update or delete
I can make this work with:
List<Account> accountList = new List<Account>();
for (SObject sObj : resultSObjectList) {
accountList.add((Account) sObj);
}
upsert accountList;
But that is ugly and I will need to make a similar implementation for every consumer of my process.
Likewise, I'd prefer not to create two separate lists (for insert and for update) and then do two separate DML operations.
Any ideas?