Batch Apex Best Practices
Batch Apex is a powerful tool in Salesforce that allows you to process large volumes of data asynchronously.
However, there are certain best practices to keep in mind to ensure optimal performance and avoid hitting governor limits.
Here are some key points to consider:
1. Avoid invoking a batch job from a trigger unless you can guarantee that it won't exceed the limit.
Be cautious when it comes to API bulk updates, import wizards, mass record changes through the UI, and any scenario where multiple records can be updated at once.
2. Keep in mind that when you call the Database.executeBatch method, the job is placed in a queue and the actual execution may be delayed based on service availability.
3. When testing your batch Apex, you can only test one execution of the execute method.
To avoid running into governor limits, use the scope parameter of the executeBatch method to limit the number of records passed into the execute method.
4. Batch Apex runs as an asynchronous process. When testing, make sure to use the startTest and stopTest methods to ensure that the batch job finishes before testing against the results.
5. Use the Database.Stateful annotation if you need to share instance member variables or data across job transactions.
Without this annotation, member variables are reset to their initial state at the start of each transaction.
6. Future methods cannot be used in classes that implement the Database.Batchable interface, and they also cannot be called from a batch Apex class.
7. When running a batch Apex job, email notifications are sent to the user who submitted the job.
If the code is in a managed package, notifications are sent to the recipient listed in the Apex Exception Notification Recipient field of the subscribing org.
8. Every method execution in batch Apex follows the standard governor limits for anonymous blocks, Visualforce controllers, or WSDL methods.
9. Each batch Apex invocation creates an AsyncApexJob record.
To retrieve the job's status, number of errors, progress, and submitter, you can construct a SOQL query using the AsyncApexJob record's ID.
10. It's recommended to filter out records of type BatchApexWorker when querying for AsyncApexJob records to avoid returning extra records for every 10,000 AsyncApexJob records.
11. All methods implemented from the Database.Batchable interface must be defined as public or global.
12. For sharing recalculation, it's recommended to delete and re-create all Apex managed sharing for batch records.
This ensures accurate and complete sharing.
13. Batch jobs queued before a Salesforce service maintenance downtime will remain in the queue and execute once resources become available after the downtime.
14. Minimize the number of batches if possible, as Salesforce uses a queue-based framework for handling asynchronous processes.
If more than 2,000 unprocessed requests from a single organization are in the queue, additional requests from that organization will be delayed.
15. To ensure fast execution of batch jobs, minimize Web service callout times and optimize queries used in your batch Apex code.
16. When using batch Apex with Database.QueryLocator to access external objects via an OData adapter for Salesforce Connect, consider enabling Request Row Counts and Server Driven Pagination on the external data source for better performance and handling of large result sets.
17. When using relationship subqueries in a QueryLocator, batch jobs will use a slower, non-chunking implementation.
It's recommended to perform the subquery separately within the execute method to utilize a faster, chunked implementation.
18. If record locking is required in the batch job, you can requery records within the execute method using FOR UPDATE to avoid conflicting updates being overwritten.
By following these best practices, you can ensure the effective and efficient execution of batch Apex in your Salesforce org.
Follow Us