1

I have this very weird error, "System.TypeException: Cannot have more than 10 chunks in a single operation", has anyone seen/encountered this before ? Please can you guide me if you know how to solve this.

I am trying to insert different types of sObjects together in a list of sObject. The list is never larger than 10 rows.

5 Answers 5

3

This post here:

https://developer.salesforce.com/forums/ForumsMain?id=906F000000090nUIAQ

suggests that it is not the number of different sObjects, but the order of the objects that causes this chunk limit to be exceeded. In other words, "1,1,1,2,2,2" has one chunk, the transition from "1" to "2". "1,2,3,4,5,6" has six chunks, even though the number of elements is the same. Putting the objects into the list sorted in object order is the suggested solution.

Is it possible for you to create a reasonable test case with only 2 or 3 rows?

2

There are two possible explanations for this issue:

  1. As Jagular noted, you did not order the sobjects you tried to insert, so there are more than 10 'chunks' in the list.
  2. You try to insert > 2000 records, and > 1 sobject type. This one seems like a Salesforce Bug, since the error message doesn't match the issue.
1
  • I know for a fact that condition 2 is my situation. Oy!!
    – sechastain
    Feb 18, 2015 at 17:16
1

Scenario 1 and its solution

When you have a hybrid list, make sure that the objects are not scattered without any order. For example, A,B,A,B,A,B,A,B…. Salesforce has an inherent trouble in switching sObject types for more than 10 times. They call this switching limit as Chunking Limit. So, on this hybrid list, if you would have sorted it and then passed it for DML, Salesforce would have been much happier. For example. A,A,A,A,B,B,B,B… In this case, salesforce only has to switch one time (that is read all A objects –>switch –> read all B objects). The max chunk limit is 10. So, here we are safe. listToUpdate.sort(); UPDATE listToUpdate;

Scenario 2 and its solution

Another point that we have to bear in our mind is that if the hybrid list contains more number of objects for one type, we can run into TypeException. As mentioned in the screenshot, if list contains 1001 objects of type A and 1001 objects of type B, then total objects is equal to 2002. The maximum chunks allowed is 10. So, if you do a simple math, the number of objects in each chunk would be 2002/10 = 200. Salesforce also enforces another governor limit that each chunk should not contain 200 or more than 200 objects. In this case, we will have to foresee how much objects are possible to enter this code and we have to write code to pass lists of safe size for DML every time. Scenario 3 and its solution

Scenario 3 and its solution

Third scenario that can happen is if the hybrid list contains objects of more than 10 types, then even if the size of the list if very small, switching happens when salesforce reads different sObject. So, we have to make sure that in this case, we allot separate lists for each sObject type and then pass it on for DML. Doing this in an apex trigger or apex class would cause you some trouble as multiple DML’s are initiated in a context. Passing this kind of multiple sObject lists for DML operations in a different context would really ease up the load you pump into the platform. Consider doing this kind of logic in a Batch Apex Job rather than a apex trigger or apex class.

Hope this helps.

1

Below a code that should cover all 3 Scenario from Arpit Sethi. It's a piece of code I took from this topic: https://developer.salesforce.com/forums/?id=906F000000090nUIAQ.

and modified to cover Scenario 2.

    private static void saveSobjectSet(List <Sobject> listToUpdate) {
    Integer SFDC_CHUNK_LIMIT = 10;

    // Developed this part due to System.TypeException: Cannot have more than 10 chunks in a single operation
    Map<String, List<Sobject>> sortedMapPerObjectType = new Map<String, List<Sobject>>();
    Map<String, Integer> numberOf200ChunkPerObject = new Map<String, Integer>();
    for (Sobject obj : listToUpdate) {
        String objTypeREAL = String.valueOf(obj.getSObjectType());

        if (! numberOf200ChunkPerObject.containsKey(objTypeREAL)){
            numberOf200ChunkPerObject.put(objTypeREAL, 1);
        }
        // Number of 200 chunk for a given Object
        Integer numnberOf200Record = numberOf200ChunkPerObject.get(objTypeREAL);
        // Object type + number of 200 records chunk
        String objTypeCURRENT = String.valueOf(obj.getSObjectType()) + String.valueOf(numnberOf200Record);
        // CurrentList
        List<sObject> currentList = sortedMapPerObjectType.get(objTypeCURRENT);

        if (currentList == null || currentList.size() > 199) {
           if(currentList != null && currentList.size() > 199){
                numberOf200ChunkPerObject.put(objTypeREAL, numnberOf200Record + 1);
                objTypeCURRENT = String.valueOf(obj.getSObjectType()) + String.valueOf(numnberOf200Record);            
            }
            sortedMapPerObjectType.put(objTypeCURRENT, new List<Sobject>());
        }
        sortedMapPerObjectType.get(objTypeCURRENT).add(obj);
    }
    while(sortedMapPerObjectType.size() > 0) {
        // Create a new list, which can contain a max of chunking limit, and sorted, so we don't get any errors 
        List<Sobject> safeListForChunking = new List<Sobject>();
        List<String> keyListSobjectType = new List<String>(sortedMapPerObjectType.keySet());
        for (Integer i = 0;i<SFDC_CHUNK_LIMIT && !sortedMapPerObjectType.isEmpty();i++) {
            List<Sobject> listSobjectOfOneType = sortedMapPerObjectType.remove(keyListSobjectType.remove(0)); 
            safeListForChunking.addAll(listSobjectOfOneType);
        }

        update safeListForChunking;
    }
}

Hope it helps, Bye

0

Hi i kind of deviced a simple way to sort a list of different sobject types

public List<Sobject> SortRecordsByType(List<Sobject> records){
    List<Sobject> response = new List<Sobject>();
    Map<string,List<Sobject>> sortDictionary = new Map<string,List<Sobject>>();
    
    for(Sobject record : records){
       string objectTypeName = record.getSobjectType().getDescribe().getName();
        if(sortDictionary.containsKey(objectTypeName)){
            sortDictionary.get(objectTypeName).add(record);
        }else{
            sortDictionary.put(objectTypeName , new List<Sobject>{record});
        }
    }
    
    // arrange in order
    for(string objectName : sortDictionary.keySet()){
        response.addAll(sortDictionary.get(objectName));
    }
    
    return response;
    
    
}

hopefully this solves your problem .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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