Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
check out this blog ( translate it to english) – Rao Sep 2 '14 at 18:21

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;
share|improve this answer

Have you tried:

database.upsert(objectUpsertList, Id, true);

share|improve this answer
Upsert DML operation, is eligible only for a single record SObject. It won't work in this case. Cheers! – Endrit Sino May 22 '15 at 12:26
Not true, from the documentation: public static Database.UpsertResult[] upsert(sObject[] recordsToUpsert, Schema.SObjectField externalIdField, Boolean allOrNone) – Davin Casey May 25 '15 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: . Thank you! – Endrit Sino May 25 '15 at 10:33
 sObject sObj = Schema.getGlobalDescribe().get('Account').newSObject();

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

 Account ResultFinal=(Account )sObj;

 upsert ResultFinal opt_external_id;
share|improve this answer
can you add more details to your answer rather than just the code to help how it addresses the question – Vamsi Krishna Mar 25 '15 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.)

Here is a workaround for the second approach:

Hope it helps! E.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.