Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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()) {
                continue;
            }
        }

        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) www30304u.sakura.ne.jp/blog/?p=1407 –  rao Sep 2 '14 at 18:21

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)
    updObjs.add(something);
  else
    insObjs.add(something);
}
insert insObjs;
update updObjs;
share|improve this answer

Have you tried:

database.upsert(objectUpsertList, Id, true);

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database.htm#apex_System_Database_upsert

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 yesterday
 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 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: http://www.sundoginteractive.com/sunblog/posts/upserts-with-generic-sobjects-salesforce

Hope it helps! E.

share|improve this answer

Your Answer

 
discard

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.