Batch apex is used to process the large no of data in Asynchronous mode.( Runs when the resources are free )
With the help of batch apex we can process upto 50 million records.
We prefer to use the batch apex when we have large set of data and we wanted to perform/execute certain logic on the scheduled basis.
Batch apex is used to process the large no of data in Asynchronous mode.( Runs when the resources are free )
With the help of batch apex we can process upto 50 million records.
We prefer to use the batch apex when we have large set of data and we wanted to perform/execute certain logic on the scheduled basis.
For Example, You are working for a telecom industry and you need to send the Email/SMS to all the mobile subscribers whose topup is going to expire in next 7 & 3 days.
Note:- Batch Apex is nothing but it is an Apex Class which is implementing an Interface provided by Salesforce
To make any class work as Batch Class you need to implement an interface Batchable which is inside Database namespace. See below example
Batchable interface have 3 main pre-defined methods which you must need to implement and those methods are
Start Method – This is the first method of batch apex which always executes once not more than once. This method is responsible for returning the List which will be passed inside execute method.
Note:- This method will always execute.
Execute Method – To do the required processing for each chunk of data, use the execute method. This method is called for each batch of records that you pass to it.
This method takes the following:
Note: – This method can be executed many times depends upon no of records returned by start method and batch size of the batch apex.
Finish Method Always executes once after all the execute chunks has been executed. In this method you can have logic to send the email, call another batch apex.
You might have noticed that Database.BatchableContext is being used in all three methods of batch apex. So with the help of Database.BatchableContext class we can track the state of the batch apex.
We can also abort the batch apex and prevent the logic from being executed.
The class have following methods
As you might have seen that start method have 2 return type and one is Database.QueryLocator.
This is the most widely return type used in batch apex and when we use Database.QueryLocator as return type then we use Database.getQueryLocatior method to execute the SOQL query and return the records/scope.
Below is the Sample structure of the batch apex without any logic.
Below is a simple batch apex which is making a query on account object and updating the account name.
As we have read that finish method executes once and can be used to Send the Email or Execute another batch apex. So we will modify the finish method to send the email to yourself stating that batch apex has been completed.
Find the complete code below
When you have completed the development. Now, the time is to execute the batch apex and text it.
To execute the batch apex we use Database.executeBatch method which accept the instance of batch class and batch size as parameters.
See below code to execute the above batch apex.
[…] the previous blog post, we talked about what is Batch Apex in Salesforce and we also have discussed about that we can schedule the batch apex to run at a […]