What is Batch Apex in Salesforce? How to Execute Batch Apex Class?
Batch Apex In Salesforce
- If you want to process a large number of records every day or within a certain time interval, you may encounter administrative restrictions.
- To resolve this governing limit issue, we will run the operation as an Asynchronous operation using batch apex.
- Batch Apex is exposed as an interface that must be implemented by developers. We can invoke at runtime batch jobs programmatically using apex.
- Batch Apex will break the large set of records into a number of batches with a small set of data and every batch will run independently from each other with a fresh set of governing limits.
- If you want any apex class to run like batch apex, that apex class has to implement an Interface “Database.Batchable”.
- Any apex class which implements Database.Batchable interface should define 3 methods:
- Start
- Execute
- Finish
Don't forget to check out: How To Use Database.Stateful Interface In Batch Apex In Salesforce
Start Method
- This method is called when the batch job starts and collects the data that needs to be operated on.
Syntax: Database.Querylocator iteration start (Database.Batchablecontext bc) {} - Database.Querylocator: When you use a simple query (SELECT...) to generate a range of records on which batch jobs should be run, use Database.Querylocator.
- If Querylocator objects are used, the total number of governors is limited.
- The number of records retrieved by the SOQL query is bypassed, and we can obtain up to 50 million records in the SOQL query.
- If you are using Iterable, all Governing limits will be still enforced.
- The start method will break the list of records into no. of batches and invoke the execute method on every batch.
Execute Method
- This method will be invoked by the START Method on every batch of records.
- This method will contain business logic that needs to be performed on the records fetched from the start method.
Syntax: Global Void Execute(Database.Batchablecontext bc,List aacScope){} - List contains the list of records in the batch job on which this execute method is running.
- Example: if the Start method fetched 1000 records and divided into 5 batches with 200 records in each batch, then execute method will be called on every batch separately (Execute method will be called 5 times)
Finish Method
- This method will be called after executing all the execute methods.
- This method is used for batch operations that send confirmation emails
Syntax: global void finish(Database. Batchablecontext qc){}
Check out another amazing blog by Kirandeep here: Salesforce to Salesforce Integration Using SOAP API
Limits of Batch Apex
- At a time only 5 batch jobs will run.
- At a time 100 jobs can be added to flexQueue.
- In a batch Job, if anyone of the batch jobs fails, only that batch will fail and the rest of the batches will execute normally.
- If the finish method fails, only the finish method will fail and changes made by the execute method will be committed.
- We cannot call future methods from batch vertices.
- We can call the batch job from the completion method of another batch job.
- If you want to call web services from the batch apex, the apex class has to implement the “Database.allowscallouts” interface.
Reference: salesforcecodes, infallibletechie, webkul, success.salesforce, sfdcpoint