I am having trouble in upserting a List. sObject may be Task or Account

public static void onTaskStatusChange(List<Task> newList, Map<Id, Task> oldMap) {
        List<SObject> objectUpsertList = new List<SObject>();

        for(Task newTask : newList) {
            Task oldTask = null;

            if(oldMap != null && oldMap.get(newTask.Id) != null) {
                oldTask = oldMap.get(newTask.Id);

            objectUpsertList = HelperClass.maintainTasks(newTask, oldTask);

            if(objectUpsertList.isEmpty()) {

        if(!objectUpsertList.isEmpty()) {
            upsert objectUpsertList;

I am having error, while wriitting Test Method for this.

System.DmlException: Update failed. First exception on row 0 with id 00Tc000000CIr9iEAD; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Task_AfterUpdate: execution of AfterUpdate

caused by: System.TypeException: DML on generic List only allowed for insert, update or delete

Is there any work around for this?


4 Answers 4


Use strictly insert or update. It's easy to found out which you should do since if there is an Id on the record it will always be update and the other way around for insert. You can even use 2 separate lists for this. 1 for update and 1 for insert. Just do a check for them.

List<SObject> updObjs = new List<SObject>();
List<SObject> insObjs = new List<SOBject>();
for (.... ...: ....)
  if (something.Id != null)
insert insObjs;
update updObjs;
 sObject sObj = Schema.getGlobalDescribe().get('Account').newSObject();

 sObj.put('name' , 'test');

 Account ResultFinal=(Account )sObj;

 upsert ResultFinal opt_external_id;
  • can you add more details to your answer rather than just the code to help how it addresses the question Commented Mar 25, 2015 at 23:44

I'm running on the same problem. And basically for this problem there are two approaces:

  1. Either you, save two(or more) collections of the target objets to upsert, and go through the SObject List, and typecast the items individually and save them to the collections.(N UPSERT statements & N Collections for N type of Objects)

  2. Or, you filter the SObjects, by detecting in Database, if they exist or not. Divide only in two different lists: soUpdateList & soInsertList. (2 DML Operations, 1 SOQL query for ever N Objects, but you can see the efficency in the memory, here.)


Have you tried:

database.upsert(objectUpsertList, Id, true);


  • Upsert DML operation, is eligible only for a single record SObject. It won't work in this case. Cheers! Commented May 22, 2015 at 12:26
  • Not true, from the documentation: public static Database.UpsertResult[] upsert(sObject[] recordsToUpsert, Schema.SObjectField externalIdField, Boolean allOrNone) Commented May 25, 2015 at 9:25
  • Actually, strange as it may sound, I have tried it, and it fails to upsert a collection of SObjects through this approach. For a quick test, please refer, to my test snippet: gist.github.com/EndritSino/710ef9cb4e8fdc43b148 . Thank you! Commented May 25, 2015 at 10:33

You must log in to answer this question.

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