In this blog post, we will learn about Batch Apex.We use Batch Apex to build complex,long-running processes which operate on thousands of records. Batch apex divides record set into manageable batches to avoid hitting the governor limits.As you already know, salesforce has governor limits on data. But when you have to retrieve thousands of records or execute DML operation on thousands of records on object,it gets complex in salesforce and you have to deal with salesforce limits.
So in this case, Batch apex comes very handy. Using batch apex, we divide those record set into multiple batches. Each batch will be processed individually.
What is Batchable Interface?
In order to use Batch Apex, we must implement Database.Batchable interface. We have to implement below methods:
Start: This method is called at the beginning of the batch apex job.This method will return either an object of type
Database.QueryLocator or an iterable that contains the records or objects passed to the job.This method will collect records on which the operation will be performed. These records are divided into batches and pass those to execute method.
Execute: this method contains the set of instructions which we want to perform on those records which we
retrieved from the Start method.
Finish: this method gets executed after all the batches are processed. We use this method to send email to inform them about the job completion.