{"id":148,"date":"2022-11-12T04:36:17","date_gmt":"2022-11-11T23:36:17","guid":{"rendered":"http:\/\/doozyspot.com\/?p=148"},"modified":"2022-11-12T04:42:35","modified_gmt":"2022-11-11T23:42:35","slug":"salesforce-apex-system-typeexception-cannot-have-more-than-10-chunks-in-a-single-operation-please-rearrange-the-data-to-reduce-chunking","status":"publish","type":"post","link":"https:\/\/doozyspot.com\/?p=148","title":{"rendered":"Salesforce Apex System.TypeException &#8211; Cannot have more than 10 chunks in single operation. <em>Please rearrange the data to reduce chunking<\/em>."},"content":{"rendered":"\n<p><strong>Solution<\/strong>: <em>Please rearrange the data to reduce chunking<\/em>.<br><br>When I am trying to insert parent and child in same transaction while preparing the list of records to be inserted in a loop, I am getting this error as System.TypeException &#8220;Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking.&#8221; <br><br>Here is sample erroring apex code snippet.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;SObject>  lstSObj = new List&lt;SObject>();\nInteger inputSize = JSonInputString.size(); \/\/ assume 10\nfor(integer i = 0; i &lt; inputSize ; i ++) {\n    lstSObj.add(new Parent(name='DoozySpot'+i, company='DoozySpot'+i));\n\n    lstSObj.add(new Child(name='DZ Child'+i));\n}\n\ninsert lstSObj;<\/code><\/pre>\n\n\n\n<p>The lstSObj is considered is having records as<br>Parent, Child, Parent, Child, Parent, Child&#8230;. 10 times.<br><\/p>\n\n\n\n<p>This is making my list contains more than 10 different Sobjects so more than 10 chunks.<br><br>This kind of error occurs when my list of sobject contains more than 10 types of object records or record of objects are not ordered based on type of sobject added to the list.<\/p>\n\n\n\n<p>For Ex:<br><br><\/p>\n\n\n\n<p>We can only create 10 separate insert chunks, and every time you alternate creates a new chunk. Consider the following list, which counts for 12 chunks instead of 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>insert new List&lt;SObject> {\n    new Account(Name='Name1'), new Contact(Name='Contact1'),\n    new Account(Name='Name2'), new Contact(Name='Contact2'),\n    new Account(Name='Name3'), new Contact(Name='Contact3'),\n    new Account(Name='Name4'), new Contact(Name='Contact4'),\n    new Account(Name='Name5'), new Contact(Name='Contact5'),\n    new Account(Name='Name6'), new Contact(Name='Contact6')\n};<\/code><\/pre>\n\n\n\n<p>We can sort out this issue 2 ways<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Add all of the similar kind of records at one after another type<\/li>\n\n\n\n<li>Sorting the records in the list.<\/li>\n<\/ol>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reorder the records related to each object while adding to the list itself<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;SObject>  lstSObj = new List&lt;SObject>();\nInteger inputSize = JSonInputString.size(); \/\/ assume 10\nfor(integer i = 0; i &lt; inputSize ; i ++) {\n    lstSObj.add(new Parent(name='DoozySpot'+i, company='DoozySpot'+i));\n}\nfor(integer i = 0; i &lt; inputSize ; i ++) {\n    lstSObj.add(new Child(name='DZ Child'+i));\n}\n\ninsert lstSObj;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sorting the records in the apex List before performing DML<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;SObject>  lstSObj = new List&lt;SObject>();\nInteger inputSize = JSonInputString.size(); \/\/ assume 10\nfor(integer i = 0; i &lt; inputSize ; i ++) {\n    lstSObj.add(new Parent(name='DoozySpot'+i, company='DoozySpot'+i));\n\n    lstSObj.add(new Child(name='DZ Child'+i));\n}\n\n\/\/Sorting before Apex DML\nlstSObj.sort();\ninsert lstSObj;<\/code><\/pre>\n\n\n\n<p>This has fixed my problem, hope it helps someone.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Solution: Please rearrange the data to reduce chunking. When I am trying to insert parent and child in same transaction while preparing the list of records to be inserted in a loop, I am getting this error as System.TypeException &#8220;Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[9],"tags":[18,20,19,5],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-apex","tag-apex-batch","tag-dml","tag-salesforce"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Salesforce Apex System.TypeException - Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking. - DoozySpot<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/doozyspot.com\/?p=148\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Salesforce Apex System.TypeException - Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking. - DoozySpot\" \/>\n<meta property=\"og:description\" content=\"Solution: Please rearrange the data to reduce chunking. When I am trying to insert parent and child in same transaction while preparing the list of records to be inserted in a loop, I am getting this error as System.TypeException &#8220;Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/doozyspot.com\/?p=148\" \/>\n<meta property=\"og:site_name\" content=\"DoozySpot\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-11T23:36:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-11T23:42:35+00:00\" \/>\n<meta name=\"author\" content=\"doozyspot\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"doozyspot\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/doozyspot.com\/?p=148#article\",\"isPartOf\":{\"@id\":\"https:\/\/doozyspot.com\/?p=148\"},\"author\":{\"name\":\"doozyspot\",\"@id\":\"https:\/\/doozyspot.com\/#\/schema\/person\/bccb52398479b1354683df33f84154d6\"},\"headline\":\"Salesforce Apex System.TypeException &#8211; Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking.\",\"datePublished\":\"2022-11-11T23:36:17+00:00\",\"dateModified\":\"2022-11-11T23:42:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/doozyspot.com\/?p=148\"},\"wordCount\":230,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/doozyspot.com\/#organization\"},\"keywords\":[\"Apex\",\"Apex Batch\",\"DML\",\"Salesforce\"],\"articleSection\":[\"Salesforce\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/doozyspot.com\/?p=148#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/doozyspot.com\/?p=148\",\"url\":\"https:\/\/doozyspot.com\/?p=148\",\"name\":\"Salesforce Apex System.TypeException - Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking. - DoozySpot\",\"isPartOf\":{\"@id\":\"https:\/\/doozyspot.com\/#website\"},\"datePublished\":\"2022-11-11T23:36:17+00:00\",\"dateModified\":\"2022-11-11T23:42:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/doozyspot.com\/?p=148#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/doozyspot.com\/?p=148\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/doozyspot.com\/?p=148#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/doozyspot.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Salesforce Apex System.TypeException &#8211; Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/doozyspot.com\/#website\",\"url\":\"https:\/\/doozyspot.com\/\",\"name\":\"DoozySpot\",\"description\":\"One of its kind in knowledge base\",\"publisher\":{\"@id\":\"https:\/\/doozyspot.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/doozyspot.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/doozyspot.com\/#organization\",\"name\":\"DoozySpot\",\"url\":\"https:\/\/doozyspot.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/doozyspot.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/doozyspot.com\/wp-content\/uploads\/2023\/03\/download.jpg\",\"contentUrl\":\"http:\/\/doozyspot.com\/wp-content\/uploads\/2023\/03\/download.jpg\",\"width\":1200,\"height\":1200,\"caption\":\"DoozySpot\"},\"image\":{\"@id\":\"https:\/\/doozyspot.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/doozyspot.com\/#\/schema\/person\/bccb52398479b1354683df33f84154d6\",\"name\":\"doozyspot\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/doozyspot.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2584f56a7b7945fd49a8fdf22dd31fa7baf3cf6f10370736f2d2895c7d9219f4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2584f56a7b7945fd49a8fdf22dd31fa7baf3cf6f10370736f2d2895c7d9219f4?s=96&d=mm&r=g\",\"caption\":\"doozyspot\"},\"sameAs\":[\"http:\/\/doozyspot.com\"],\"url\":\"https:\/\/doozyspot.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Salesforce Apex System.TypeException - Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking. - DoozySpot","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/doozyspot.com\/?p=148","og_locale":"en_US","og_type":"article","og_title":"Salesforce Apex System.TypeException - Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking. - DoozySpot","og_description":"Solution: Please rearrange the data to reduce chunking. When I am trying to insert parent and child in same transaction while preparing the list of records to be inserted in a loop, I am getting this error as System.TypeException &#8220;Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce [&hellip;]","og_url":"https:\/\/doozyspot.com\/?p=148","og_site_name":"DoozySpot","article_published_time":"2022-11-11T23:36:17+00:00","article_modified_time":"2022-11-11T23:42:35+00:00","author":"doozyspot","twitter_card":"summary_large_image","twitter_misc":{"Written by":"doozyspot","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/doozyspot.com\/?p=148#article","isPartOf":{"@id":"https:\/\/doozyspot.com\/?p=148"},"author":{"name":"doozyspot","@id":"https:\/\/doozyspot.com\/#\/schema\/person\/bccb52398479b1354683df33f84154d6"},"headline":"Salesforce Apex System.TypeException &#8211; Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking.","datePublished":"2022-11-11T23:36:17+00:00","dateModified":"2022-11-11T23:42:35+00:00","mainEntityOfPage":{"@id":"https:\/\/doozyspot.com\/?p=148"},"wordCount":230,"commentCount":0,"publisher":{"@id":"https:\/\/doozyspot.com\/#organization"},"keywords":["Apex","Apex Batch","DML","Salesforce"],"articleSection":["Salesforce"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/doozyspot.com\/?p=148#respond"]}]},{"@type":"WebPage","@id":"https:\/\/doozyspot.com\/?p=148","url":"https:\/\/doozyspot.com\/?p=148","name":"Salesforce Apex System.TypeException - Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking. - DoozySpot","isPartOf":{"@id":"https:\/\/doozyspot.com\/#website"},"datePublished":"2022-11-11T23:36:17+00:00","dateModified":"2022-11-11T23:42:35+00:00","breadcrumb":{"@id":"https:\/\/doozyspot.com\/?p=148#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/doozyspot.com\/?p=148"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/doozyspot.com\/?p=148#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/doozyspot.com\/"},{"@type":"ListItem","position":2,"name":"Salesforce Apex System.TypeException &#8211; Cannot have more than 10 chunks in single operation. Please rearrange the data to reduce chunking."}]},{"@type":"WebSite","@id":"https:\/\/doozyspot.com\/#website","url":"https:\/\/doozyspot.com\/","name":"DoozySpot","description":"One of its kind in knowledge base","publisher":{"@id":"https:\/\/doozyspot.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/doozyspot.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/doozyspot.com\/#organization","name":"DoozySpot","url":"https:\/\/doozyspot.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/doozyspot.com\/#\/schema\/logo\/image\/","url":"http:\/\/doozyspot.com\/wp-content\/uploads\/2023\/03\/download.jpg","contentUrl":"http:\/\/doozyspot.com\/wp-content\/uploads\/2023\/03\/download.jpg","width":1200,"height":1200,"caption":"DoozySpot"},"image":{"@id":"https:\/\/doozyspot.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/doozyspot.com\/#\/schema\/person\/bccb52398479b1354683df33f84154d6","name":"doozyspot","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/doozyspot.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2584f56a7b7945fd49a8fdf22dd31fa7baf3cf6f10370736f2d2895c7d9219f4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2584f56a7b7945fd49a8fdf22dd31fa7baf3cf6f10370736f2d2895c7d9219f4?s=96&d=mm&r=g","caption":"doozyspot"},"sameAs":["http:\/\/doozyspot.com"],"url":"https:\/\/doozyspot.com\/?author=1"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/doozyspot.com\/index.php?rest_route=\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doozyspot.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/doozyspot.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/doozyspot.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doozyspot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=148"}],"version-history":[{"count":6,"href":"https:\/\/doozyspot.com\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":157,"href":"https:\/\/doozyspot.com\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions\/157"}],"wp:attachment":[{"href":"https:\/\/doozyspot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/doozyspot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/doozyspot.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}