{"id":2562,"date":"2023-11-22T07:16:43","date_gmt":"2023-11-22T07:16:43","guid":{"rendered":"https:\/\/www.emizentech.com\/blog\/?p=2562"},"modified":"2023-11-22T07:16:47","modified_gmt":"2023-11-22T07:16:47","slug":"call-batch-apex-from-another-batch-apex","status":"publish","type":"post","link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/call-batch-apex-from-another-batch-apex.html","title":{"rendered":"How To Invoke Batch Apex From Another Batch Apex In Salesforce"},"content":{"rendered":"\n<p><br>Managing large data sets and handling complex processes efficiently are key skills in Salesforce development. This helps improve performance and ensures a smooth experience for users. Our guide offers easy-to-follow tips if you&#8217;re new to Salesforce or looking to improve your skills.<\/p>\n\n\n\n<p>In Lightning Web Components (LWC) development, being able to dynamically style components is really helpful. This means you can change how things look based on what users do, making your website or app more interactive and user-friendly. We&#8217;ll focus on how to change and set classes in LWC using JavaScript.<\/p>\n\n\n\n<p>Typically, LWC uses a CSS file with the same name as the component for styling. For example, a &#8216;paginator&#8217; component would use a paginator.css file. But sometimes, you need more flexible styling. <\/p>\n\n\n\n<p>Remember, our Salesforce experts at Emizentech are always here to help with any questions. Let&#8217;s explore dynamic styling in LWC and boost your Salesforce development know-how.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_is_it_possible\"><\/span>How is it possible?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are two ways in Salesforce which are used to call the batch class from another batch class are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using Queueable Apex<\/li>\n\n\n\n<li>Using the Finish method of Batch class.<\/li>\n<\/ul>\n\n\n\n<p>Using this way, you can create the chaining between the batches.<\/p>\n\n\n\n<p><strong>Note:<\/strong> If you call the batch class from the start or execute methods, then Salesforce throws the below error:<br>System.AsyncException: Database.executeBatch cannot be called from a batch start, batch execute, or future method.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/implement-sso-using-oauth-for-salesforce.html\" target=\"_blank\" rel=\"noopener\">How to Implement Salesforce SSO Using OAuth?<\/a><\/h4>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/1-batch-execute-error.png\" alt=\"batch execute error\" class=\"wp-image-2567\"\/><\/figure><\/div>\n\n\n<p>While calling batch from the start method there is an error:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/2-execute-method-of-batch.png\" alt=\"execute method of batch\" class=\"wp-image-2568\"\/><\/figure><\/div>\n\n\n<p>Same error while calling from the execute method of batch:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Here is the complete definition of an error message:<img fetchpriority=\"high\" decoding=\"async\" width=\"1361\" height=\"412\" class=\"aligncenter wp-image-2569 size-full\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/3-complete-definition-of-error-message.png\" alt=\"complete definition of error message\"><br>Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/pipedrive-salesforce-integration.html\" target=\"_blank\" rel=\"noopener\">How To Connect Pipedrive and Salesforce Integration?<\/a><\/h4>\n\n\n\n<p><strong>There Are Two Solutions for This Error:<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_Queueable_Apex\"><\/span>1. Using Queueable Apex:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/4-Queueable-Apex.png\" alt=\"Queueable Apex\" class=\"wp-image-2570\"\/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/5-using-Queueable-Apex.png\" alt=\"using Queueable Apex\" class=\"wp-image-2571\"\/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_the_Finish_Method_of_Batch_Class\"><\/span>2. Using the Finish Method of Batch Class:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Batch Class Example:<\/h4>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/6-Finish-Method-of-Batch-Class.png\" alt=\"Finish Method of Batch Class\" class=\"wp-image-2572\"\/><\/figure><\/div>\n\n\n<pre class=\"wp-block-code\"><code>global class BatchAccountUpdate implements Database.Batchable&lt;sObject&gt;, Database.Stateful, Database.AllowsCallouts {\n String query = 'Select Name from Account WHERE Name != null AND (Name = \\'Sapna Company\\' OR Name = \\'Sapna\\') ';\n global Database.QueryLocator start(Database.BatchableContext bc) {\n \/\/ collect the batches of records or objects to be passed to execute\n return Database.getQueryLocator(query);\n }\n \n global void execute(Database.BatchableContext bc, List&lt;Account&gt; records){\n \/\/ process each batch of records\n for(Account acc : records){\n acc.Name = acc.Name + ' - Updated';\n }\n update records;\n }\n \n global void finish(Database.BatchableContext bc){\n \/\/ execute any post-processing operations\n Note: We are calling Batch from Finish Method of Second batch.\n BatchUpdateAccountRelatedContacts b = new BatchUpdateAccountRelatedContacts();\n Database.executeBatch(b, 200);\n }\n }<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/multisitelocal.ezxdemo.com\/contact-us.html?utm_source=blog&amp;utm_medium=banner&amp;utm_campaign=emizen_blog\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/hire-salesforce-developers-1.png\" alt=\"hire salesforce developers\" class=\"wp-image-2389\"\/><\/a><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">Calling from Schedule Class:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>global class ScheduleBatchAccountUpdate implements Schedulable {\n global void execute(SchedulableContext sc) {\n BatchAccountUpdate b = new BatchAccountUpdate();\n database.executebatch(b);\n }\n }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Second Batch:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>global class BatchUpdateAccountRelatedContacts implements Database.Batchable&lt;sObject&gt;, Database.Stateful, Database.AllowsCallouts {\n String query = 'Select FirstName, LastName, Email, Status__c from Contact WHERE\n FirstName != null AND LastName != null AND AccountId != null';\n global Database.QueryLocator start(Database.BatchableContext bc) {\n \/\/ collect the batches of records or objects to be passed to execute\n Note: Calling Queueable job for calling the next batchfor chaining.\n System.enqueueJob(new ExecuteBatchQueueableJob());\n return Database.getQueryLocator(query);\n }\n global void execute(Database.BatchableContext bc, List&lt;Contact&gt; records){\n \/\/ process each batch of records\n for(Contact con : records){\n con.Status__c = 'Discovery Call Complete';\n }\n update records;\n }\n global void finish(Database.BatchableContext bc){\n \/\/ execute any post-processing operations\n }\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Queueable Apex:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ExecuteBatchQueueableJob implements Queueable {\n public void execute(QueueableContext context) {\n try{\n BatchCalledThroughQueueable b = new BatchCalledThroughQueueable();\n Database.executeBatch(b, 200);\n }catch(Exception e){\n System.debug('@@@ Error Message : '+e.getMessage());\n }\n }\n }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Third Batch:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>global class BatchCalledThroughQueueable implements Database.Batchable&lt;sObject&gt;, Database.Stateful, Database.AllowsCallouts {\n String query = 'Select FirstName, LastName, Email, Status, Title from Lead WHERE FirstName != null AND LastName != null';\n global Database.QueryLocator start(Database.BatchableContext bc) {\n \/\/ collect the batches of records or objects to be passed to execute\n return Database.getQueryLocator(query);\n }\n global void execute(Database.BatchableContext bc, List&lt;Lead&gt; records){\n \/\/ process each batch of records\n for(Lead ld : records){\n ld.Title = ld.FirstName + ' - ' + ld.LastName;\n }\n update records;\n }\n global void finish(Database.BatchableContext bc){\n \/\/ execute any post-processing operations\n }\n }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Also Read: <a href=\"https:\/\/multisitelocal.ezxdemo.com\/blog\/create-salesforce-lightning-map-component.html\" target=\"_blank\" rel=\"noopener\">How to Create a Salesforce Lightning Map Component?<\/a><\/h4>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We hope this guide has provided you with valuable insights and practical steps to enhance your Salesforce development projects. If you have any questions or require more in-depth guidance, don&#8217;t hesitate to contact our <a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce-consulting.html\"><strong>Salesforce Consultants<\/strong> <\/a>at Emizentech, one of the <strong><a href=\"https:\/\/multisitelocal.ezxdemo.com\/salesforce.html\">Best Salesforce Development Companies<\/a><\/strong>. Our team is dedicated to helping you achieve your development goals and ensure your Salesforce solutions are optimized for success. Contact us today for personalized assistance and expert solutions!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing large data sets and handling complex processes efficiently are key skills in Salesforce development. This helps improve performance and ensures a smooth experience for users. Our guide offers easy-to-follow tips if you&#8217;re new to Salesforce or looking to improve your skills. In Lightning Web Components (LWC) development, being able to dynamically style components is<\/p>\n","protected":false},"author":39,"featured_media":40493,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"MSN_Categories":"Uncategorized","MSN_Publish_Option":false,"MSN_Is_Local_News":false,"MSN_Is_AIAC_Included":"Empty","MSN_Location":"[]","MSN_Add_Feature_Img_On_Top_Of_Post":false,"MSN_Has_Custom_Author":false,"MSN_Custom_Author":"","MSN_Has_Custom_Canonical_Url":false,"MSN_Custom_Canonical_Url":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[87],"tags":[],"class_list":{"0":"post-2562","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-salesforce-development"},"modified_by":"Marketing EmizenTech","featured_image_src":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/Invoke-Batch-Apex-From-Another-Batch-Apex-In-Salesforce-600x400.jpg","featured_image_src_square":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-content\/uploads\/sites\/2\/2020\/05\/Invoke-Batch-Apex-From-Another-Batch-Apex-In-Salesforce-600x408.jpg","author_info":{"display_name":"Virendra Sharma","author_link":"https:\/\/multisitelocal.ezxdemo.com\/blog\/author\/salesforce"},"_links":{"self":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/comments?post=2562"}],"version-history":[{"count":0,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/posts\/2562\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media\/40493"}],"wp:attachment":[{"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/media?parent=2562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/categories?post=2562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisitelocal.ezxdemo.com\/blog\/wp-json\/wp\/v2\/tags?post=2562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}