anusha(salesforce developer)

why does ' Cannot have more than 10 chunks in a single operation ' error occurs in Salesforce

 This error occurs when sObject contains more then 10  object types or combinations.
 Most Probably while adding objects to sObject lists in  loops cause this error.


Example:
   
    List<SObject> sList=new List<Sobject>();


   sList.add(Object1);
   sList.add(object2);
   sList.add(Object1);
   sList.add(object2);
   sList.add(Object1);
   sList.add(object2);
   sList.add(Object1);
   sList.add(object2);
   sList.add(Object1);
   sList.add(object2);

 //even though two types of objects are getting added to Sobjects ,SObject consider them as different types of Objects
 and throws the error 'Cannot have more than 10 chunks in a single operation'.

Solution:


    Make Collections of  Respective objects i.e

   List<Object1> list1=new List<object1>();
   List<Object2> list2=new List<object2>();
 
   //now add objects to respective collections and finally add them to SObjects list using addall method.
   //we can't add objects list directly to sList, so convert them to sObjectlist .
 
   public list<Sobject> convertTosObject(List<sObject> list){
             return list; 
   }
 
   sList.addAll(convertTosObject(list1));
   sList.addAll(convertTosObject(list2));
 
   //now you can perform dml operations on sObject list.

   insert  sList;

No comments:

Post a Comment