<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>data &#8211; Hamza Siddiqui</title>
	<atom:link href="https://www.mhamzas.com/blog/tag/data/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mhamzas.com</link>
	<description>4x Salesforce MVP &#124; 26x Certified &#124; Salesforce App &#38; System Architect</description>
	<lastBuildDate>Fri, 03 Sep 2021 14:15:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
<site xmlns="com-wordpress:feed-additions:1">233526040</site>	<item>
		<title>Apex Data Chunking</title>
		<link>https://www.mhamzas.com/blog/2021/09/03/apex-data-chunking/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apex-data-chunking</link>
					<comments>https://www.mhamzas.com/blog/2021/09/03/apex-data-chunking/#respond</comments>
		
		<dc:creator><![CDATA[hamza]]></dc:creator>
		<pubDate>Fri, 03 Sep 2021 14:13:20 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[apex]]></category>
		<category><![CDATA[data]]></category>
		<guid isPermaLink="false">https://www.mhamzas.com/?p=3259</guid>

					<description><![CDATA[When I execute the following code You receive the following error:Execution Failed. System.TypeException: Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking. but after execution of this script: everything goes smoothly. This is because <br /><a href="https://www.mhamzas.com/blog/2021/09/03/apex-data-chunking/" class="more-link btn btn-primary">Read More</a>]]></description>
										<content:encoded><![CDATA[
<p>When I execute the following code</p>



<pre class="wp-block-code"><code>List&lt;SObject> toSave = new List&lt;SObject>();
for(integer i = 0; i &lt; 100; i++ )
{
    BB_A__c bba = new BB_A__c(
        x__c = 'testx',
        y__c = 'testy'
    );
    if(i == 5)
    {
        bba.x__c = 'bartek';
    }
    toSave.add(bba);
     
    BB_B__c bbb = new BB_B__c(
        x__c = 'testx',
        y__c = 'testy'
    );
    if(i == 7)
    {
        bba.y__c = 'bartek';
    }
    toSave.add(bbb);    
}
insert toSave;</code></pre>



<p>You receive the following error:<br><em>Execution Failed. System.TypeException: Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking.</em></p>



<p>but after execution of this script:</p>



<pre class="wp-block-code"><code>List&lt;SObject> toSave = new List&lt;SObject>();
for(integer i = 0; i &lt; 100; i++ )
{
    BB_A__c bba = new BB_A__c(
        x__c = 'testx',
        y__c = 'testy'
    );
    if(i == 5)
    {
        bba.x__c = 'bartek';
    }
    toSave.add(bba);
}
 
for(integer i = 0; i &lt; 100; i++ )
{   
    BB_B__c bbb = new BB_B__c(
        x__c = 'testx',
        y__c = 'testy'
    );
    if(i == 7)
    {
        bbb.y__c = 'bartek';
    }
    toSave.add(bbb);
     
}
insert toSave;</code></pre>



<p>everything goes smoothly. This is because when you insert a list with multiple objects, the data type cannot change more than 10 times. Do not ask why, this is SALESFORCE.<br>But there is a simple solution:<br>You have to sort the list that you want to add. Because the standard List.sort() method sorts collection by SObject labels then you will have all objects grouped by SObject label that equals to sobject type:). Here is a working example:</p>



<pre class="wp-block-code"><code>List&lt;SObject> toSave = new List&lt;SObject>();
for(integer i = 0; i &lt; 100; i++ )
{
    BB_A__c bba = new BB_A__c(
        x__c = 'testx',
        y__c = 'testy'
    );
    if(i == 5)
    {
        bba.x__c = 'bartek';
    }
    toSave.add(bba);
     
    BB_B__c bbb = new BB_B__c(
        x__c = 'testx',
        y__c = 'testy'
    );
    if(i == 7)
    {
        bba.y__c = 'bartek';
    }
    toSave.add(bbb);
     
}
 
toSave.sort(); // &lt;-- this line fixes our issue:):):)
 
insert toSave;</code></pre>



<h2 class="wp-block-heading">Conclusion</h2>



<p>Here is the method I found one of the answers from the Salesforce <a href="https://developer.salesforce.com/forums/?id=906F000000090nUIAQ" target="_blank" rel="noreferrer noopener">developers community</a> by Julien Castelluci</p>



<script src="https://gist.github.com/mhamzas/ac7480e9058ac7b06281ef6f5a60526f.js"></script>



<p><strong>Source</strong>: <br>https://bartoszborowiec.wordpress.com/2014/06/15/execution-failed-system-typeexception-cannot-have-more-than-10-chunks-in-a-single-operation-please-rearrange-the-data-to-reduce-chunking/</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mhamzas.com/blog/2021/09/03/apex-data-chunking/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3259</post-id>	</item>
	</channel>
</rss>
