To learn how to write batch classes in Salesforce, let us first understand what is batch apex class, when to use Batch Apex, and best practices for writing a batch apex class. Then we will discuss the syntax for Batch Apex class in detail so that you can learn practical concepts too.
As the name suggests, the batch word is used when you want to process a bulk amount of data effectively without any error that you need to apply some logic certainly. The Batch Apex class can be used conveniently here to perform a variety of tasks in real-time like data cleaning, data archiving, data analytics to maintain the quality. In simple words, Batch Apex classes are designed specially to process voluminous data or records together and it has maximum governor limits than the synchronous code.
|Area||Normal Apex||Batch Apex|
|SOQL Queries||100 records per cycle||200 records per cycle|
|Records retrieval by SOQL Queries||50,000||50,000,000|
Batch Apex is an essential requirement when you wanted to execute bulk records, if you are using regular apex classes then chances of errors are higher and it may hit the governor limits too. A maximum of 200 records can be passed to every batch here. The concept has plenty of awesome advantages that you should check before moving ahead –
Before you write batch apex code, you should focus on these governor limits for effective dealing with classes.
To write a batch APEX class, you should first implement the “Database.Batchable” interface in the Salesforce and including the execution of these three methods.
This method is used to collect objects or records that need to be passed to an interface method for processing. This method is called at the beginning when Batch apex starts to execute. In most of the cases, the “QueryLocator” method is used to play with the simple SOQL query and to generate the scope of objects within a batch job. In case, you wanted to do something crazy then call the API records before they are passed for the execution. Further, if you wanted to set the governor limits for a total number of records retrieved by the SOQL queries then you can query up to 50 million records together. With the help of an iterable object, the governor limit for a total number of records is retrieved by SOQL queries through enforcement.
This method will perform the actual processing for each batch or chunk where data needs to be passed with the help of a method. The default size defined for each batch is 200 records maximum. If the size of recordset is larger, break it down into manageable pieces first before you execute them. If the size is not defined well then records will not be executed in the same order as they were received by the start method.
This method is responsible to perform post-processing operations like email sending or more once all batches are executed successfully. Here is the basic syntax of a batch APEX class how are they written in the Salesforce.
Learn Salesforce in the Easiest Way
A batch apex class is usually stateless and execution of each job in the batch is considered a discrete transaction. For example, if some batch class contains more than 1000 records then based on the default size concept total five transactions will be performed here with a recordset of 200 each. Here you need to specify the Database. The stateful method in the class definition to maintain the state across all transactions. With the help of this method, the member variables could always retain their values between multiple transactions. You must be thinking why it is necessary but maintaining state is beneficial to summarize records as they are processed. For example, when we are updating contact records in our batch then you wanted to keep track of the total records affected so that the same can be included for the notification email list. Now you know how to write a batch class with the help of three methods and defining state for the database. Don’t forget to practice a few examples to get a better idea of the concept. Keep in mind that APEX development and testing go hand to hand. Every time you add some records, call the batch APEX class and check if contacts are updated properly or not as per the specified requirements.
As discussed earlier, there are always certain things you should keep in mind when using the Batch Apex class. To make sure that batch jobs are executing faster, you should minimize the callout times for web services and tune the queries used with your batch APEX code. The longer the batch size would be, the more likely other job queues will be delayed when too many jobs added together. Here are the best practices to follow by Salesforce Developers:
With this discussion, you have covered almost every concept related to batch apex class. Now, you can use it successfully in your next project and don’t forget to define governor limits well otherwise it may be annoying at the end. Today, batch apex class usage is growing tremendously and Salesforce developers are also excited about its benefits.
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
MS SQL Server
Receive Latest Materials and Offers on Salesforce Course