Batch Apex can run long-running processes that run on thousands of records on the Lightning Platform. Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks.
Batch jobs can also be programmatically scheduled to run at specific times using the Apex scheduler, or scheduled using the Schedule Apex page in the Salesforce user interface
You can only have five queued or active batch jobs at one time.
Batch Class interview question
- What is Batch Apex, and why do we use it?
- Answer: Batch Apex is a Salesforce feature used for processing large datasets asynchronously. It is beneficial when dealing with sizable data volumes and allows for the execution of tasks without immediate results. It is suitable for tasks like weekly or overnight jobs, data backup, and periodic operations like deactivating users.
- How does Batch Apex compare to other asynchronous options?
- Answer: Batch Apex is not necessarily better than other asynchronous options, such as Future Methods and Queueable Apex. Each has its own use case. Future Methods and Queueable Apex are also powerful tools, and the choice depends on the specific requirements of the task at hand.
- What are the methods available in the database.batchable interface, and what is the default batch size?
- Answer: The
database.batchableinterface in Salesforce has three methods:
finish. The default batch size is 200 records, with a maximum limit of 2000.
- Answer: The
- How do you schedule a Batch Apex job?
- Answer: Batch Apex jobs can be scheduled either through the Salesforce UI or programmatically using Apex code, such as anonymous blocks or Schedule Apex.
- Why use Batch Apex instead of Data Loader for bulk data operations?
- Answer: While Data Loader works on static datasets, Batch Apex allows dynamic creation and querying of datasets, making it more flexible for processing large datasets with changing conditions.
- What is the purpose of the database.batchable interface’s start, execute, and finish methods?
- Answer: The
startmethod retrieves the dataset, the
executemethod processes the data, and the
finishmethod handles post-processing logic. All three methods are crucial for the proper functioning of a Batch Apex class.
- Answer: The
- How do you track the status of a running Batch Apex job?
- Answer: The Batchable context variable stores information, including the job ID. You can use the job ID to query the AsyncApexJob object and track the status of the job.
- What is the purpose of the AsyncApexJob object?
- Answer: The AsyncApexJob object in Salesforce stores information about asynchronous Apex jobs, including details like job status, completion time, and errors.
- Can you call another Batch Apex from within a Batch Apex?
- Answer: Yes, you can call another Batch Apex from the finish method of a Batch Apex class.
- How many callouts are possible from a Batch Apex to external systems in a 24-hour period?
- Answer: Batch Apex allows up to 50,000 callouts in a 24-hour period.
- Can Batch Apex call Queueable Apex?
- Answer: Yes, Batch Apex can call Queueable Apex.
- Is it possible to invoke a Schedulable Apex class from Batch Apex?
- Answer: Yes, Batch Apex can invoke a Schedulable Apex class.
- Can Batch Apex publish an event in Salesforce?
- Answer: Yes, Batch Apex has the capability to publish events in Salesforce
Scenario based questions on Batch Apex
- Question: How should you approach a scenario where accounts have multiple contacts, and there are duplicate contacts that need to be removed?
- Answer: The approach should involve retrieving accounts in the start method and querying contacts in the execute method. Avoid doing the subquery in the start method itself. Asking follow-up questions is crucial to understanding the scenario properly.
- Question: If asked to write a batch to delete old cases, what follow-up questions should you ask?
- Answer: Important follow-up questions include clarifying the definition of “old cases,” whether to delete child cases, and whether to delete active cases. The logic revolves around obtaining the data set, querying it in the start method, and executing the logic.
- Question: How does
database.saveResultcontribute to processing records in Batch Apex?
database.saveResultprovides information on success and errors during record processing. It allows iterating over both success and error records and defining actions based on the results.
- Question: How can a batch be scheduled to run every half an hour?
- Answer: To execute a batch every half an hour, a cron expression needs to be created, and it should be passed to the schedulable class.
- Question: Can data be directly queried in the execute method without using the start method?
- Answer: Querying data directly in the execute method without utilizing the start method is not recommended in Batch Apex. The start method is designed to retrieve the data set, and the execute method processes it.
- Question: What is the recommended approach if the start method gets data from an external object?
- Answer: In such a scenario, the recommended approach is to create the data set in the start method as intended and follow the standard Batch Apex processing flow.
- Question: How can a batch be modified if it already has a pending job in the queue?
- Answer: Modifying a batch with a pending job in the queue involves considering governor limits and carefully managing the modification process.
- Question: How can a validation rule be bypassed during batch processing?
- Answer: Bypassing a validation rule during batch processing may involve using a variable (e.g., checkbox) that can be toggled on and off strategically in the execute method.
- Question: Is it possible to write a batch with an empty start method?
- Answer: No, it is not recommended to have an empty start method in Batch Apex. The start method is expected to return a data set for further processing.
- Question: Can callouts be made directly from the start method, and what should be considered?
- Answer: While callouts can be made from the start method, it is not advisable due to governor limits. The start method is primarily meant for creating the data set for a large dataset.