Monday 14 December 2015

How to deal with: "System.TypeException: Cannot have more than 10 chunks in a single operation" in SFDC

In one of the interfaces, I designed and developed, between SFDC & SAP/PI, there was once incident where an end user encountered that her pricing agreement is not pushed to SAP.
When I looked into the Integration Logger object, I find this error message for the first time.


"Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking.StackTrace:: Class.SfdcSapPricingInterface_Async.execute: line 2120, column 1"





When you go and look into SFDC document related to preparing list of SObjects, you will find that SFDC enforces a governor limit on the way it chunks different data types in the list<SObject>. Subsequently the number of data types that can be DML operated is limited to 10.


The order of the items in this list seems to play a part in the 'chunking'. It chunks everytime you switch object types.


Let's consider an example here. Keeping the same number of items in the list, If you are passing 12 items in a list of types A and B, this list contains 2 chunks:

          A, A, A, A, A, A, B, B, B, B, B, B  (which will not exceed this limit)

But this list contains 12 chunks:

          A, B, A, B, A, B, A, B, A, B, A, B  (which will exceed this limit)


You can find multiple solutions to this issue on the community forums et al. However I think a simple solution is this issue can be this:

     Just before your DML statement, sort your list of SObject:

               listSObjects.sort()
               insert/update listSObjects ;

This will align data of one type at one place together and other data type at other place. So in fact the chunking is now defined by number of data type present in list of SObject which can at max be 10.


Happy Apex coding   :) 

No comments:

Post a Comment

Thank you for visiting. Your comments are highly appreciated.