Development
Salesforce Batch Apex Interview Questions and Answers Experienced
1. What is Batch Apex in Salesforce?
Ans: Batch Apex allows you to process large volumes of records asynchronously by breaking them into manageable chunks called batches. It helps avoid governor limits by processing records in smaller transactions.
2. What interface must a class implement to be used as Batch Apex?
Ans:
The class must implement the Database.Batchable<SObject>
interface.
3. What are the three methods you must implement in a Batch Apex class?
Ans:
The methods are start()
, execute()
, and finish()
.
4. What is the purpose of the start()
method in Batch Apex?
Ans:
The start()
method collects the records or objects to be processed and returns a query locator or iterable for processing.
5. How does the execute()
method work in Batch Apex?
Ans:
The execute()
method processes each batch of records passed from the start()
method. It contains the logic for what to do with each batch.
6. What is the role of the finish()
method in Batch Apex?
Ans:
The finish()
method executes after all batches are processed. It can be used for post-processing, such as sending notifications or chaining another batch.
7. What is the acceptable batch size range in Batch Apex?
Ans: Batch size can range from 1 to 2,000 records per batch.
8. Can you schedule a Batch Apex job to run at a specific time?
Ans:
Yes, by implementing the Schedulable
interface and scheduling the batch class.
9. What is batch chaining in Salesforce?
Ans:
Batch chaining is when a batch job starts another batch job in its finish()
method to process large or sequential tasks.
10. What is the use of the Database.Stateful
interface in Batch Apex?
Ans: It preserves the state (variables) across batch transactions, allowing you to keep data accumulated over multiple batches.
11. Scenario: You want to update millions of Account records but avoid hitting governor limits. How would you use Batch Apex?
Ans:
Write a Batch Apex class implementing Database.Batchable
, split the records into batches (e.g., 2000), and process them asynchronously to handle large volume efficiently.
12. What happens if an unhandled exception occurs in the execute()
method of a batch job?
Ans: The current batch transaction fails, and Salesforce retries the batch job up to three times before marking it as failed.
13. Can a Batch Apex job run concurrently with another batch job?
Ans: Yes, Salesforce allows up to 5 concurrent batch jobs running at the same time per org.
14. How do you monitor Batch Apex jobs?
Ans: You can monitor batch jobs from the Salesforce Setup under "Apex Jobs" or by using the System Jobs page in the UI.
15. Scenario: You want to send an email after batch processing completes. Where would you put the email logic?
Ans:
Place the email sending logic inside the finish()
method of the Batch Apex class.
16. Can Batch Apex classes accept parameters?
Ans: Yes, you can pass parameters through the class constructor when instantiating the batch class.
17. Scenario: You want to chain multiple batch jobs where each depends on the previous. How do you implement this?
Ans:
In the finish()
method of the first batch, start the next batch job using Database.executeBatch()
.
18. What is the maximum number of batch jobs you can have queued or active concurrently?
Ans: The maximum number is 100 batch jobs queued or active at one time per org.
19. Can Batch Apex be tested in Salesforce? If yes, how?
Ans:
Yes, Batch Apex can be tested using Apex test methods by calling Test.startTest()
, invoking the batch via Database.executeBatch()
, and then Test.stopTest()
to execute asynchronous code synchronously.
20. Scenario: You need to preserve data from previous batch executions to summarize after all batches finish. What interface do you use?
Ans:
Implement the Database.Stateful
interface to maintain instance variables across transactions.