Salesforce Developer Interview Questions – Asynchronous Apex – Batch Apex
Table of Contents
104. What is a batch apex?
Batch apex is one of the asynchronous apex features or type.
Batch Apex is used to run large jobs (think thousands or millions of records!) that would exceed normal processing limits.
Using Batch Apex, we can process records asynchronously in batches (hence the name, “Batch Apex”) to stay within platform limits.
If we have a lot of records to process, for example, data cleansing or archiving, Batch Apex is probably the solution
105. How to use a batch apex?
To use batch apex, write an apex class that implements the salesforce provided interface Database.Batchable and then invoke the class programmatically.
106. Why do we implement Database.batchable interface or what happens when we implement Database.batchable interface?
Database.Batchable interface contains three methods that must be implemented.
These three methods are mandatory if we use Database.Batchable interface. If we do not implement any one of the three methods then the class throws an error.
107. Can you explain about three methods of batch apex?
Start method: It is used to collect the records of objects to pass to the interface method “execute”, call the start method at the beginning of a batch apex job. This method returns either a Database.QueryLocator object or an iterable that contains the records or objects passed to the job
Execute method: It is used to do the required processing for each chunk of data. This method is called for each batch of records that you pass to it.
Batches of records tend to execute in the order in which they’re received from the “start” method. However, the order in which batches or records execute depends on various factors. The order of execution isn’t guaranteed.
Finish method: This method is called after all batches are processed. It can be used to send confirmation emails or execute post-processing operations.
108. Can you write a sample Batch apex class?
109. Explain about Database.Querylocator in batch apex?
Database.querylocator can be used for simple query (SELECT) to generate the scope of the objects in the batch job.
With Querylocator the governor limit for the total number of records retrieved by SOQL queries is bypassed.
When using querylocator the maximum number of records can be retrieved is up to 50 millions.
110. Explain about Iterable in batch apex?
Iterable is used when we have to create a more complex scope for the batch job.
Example if the batch class needs to operate on data obtained from two unrelated objects, then we can create a custom iterable class and return combined data from both objects as a list of iterable from start method.
With Iterable we can pass both the objects or records to the execute method for processing.
With iterable the governor limit for the total number for records retrieved by SOQL queries is still enforced which is up to 50 thousand records
111. How to invoke a batch Class?
To invoke a batch class, simply instantiate it and then call Database.executeBAtch with the instance
Optionally we can pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch.
112. Can we track the progress of the batch job?
Yes. Each batch apex invocation creates an AsyncApexJob record so that we can track the job’s progress.
We can view the progress via SOQL or manage you job in the Apex Job Queue,
Apex Job Queue : Step → Jobs → Apex Jobs