Database.executeBatch: How to invoke batch apex job? / How to execute apex job programmatically?
There are two Database class methods available to execute/invoke batch apex job. Those are:
- executeBatch(batchClassObject) : This method submits a batch apex job for execution corresponding to the specified class.
- executeBatch(batchClassObject, scope) : Submits a batch Apex job for execution using the the specified class and scope.Here scope specifies Number of records to be passed into the execute method for batch processing.
Above two methods are static methods of database class. The class name which we are passing to the executeBatch()
method should be object of the class which has implemented from Database.Batchable
interface.
Order of execution of Batch Apex Job
Following is the order of execution of Batch Apex Job.
- Creates an object for the class which has implemented by using
Database.Batchable
interface. - Pass the object which you have created in the first step as a parameter to the
Database.executeBatch
method. When this method calls it will add apex job to the queue. - Once a resource is available in the queue automatically start() method will be invoked and it will collect all the records.
- The records that are fetched from the start method are divided into a small group based on the size we mentioned in the executeBatch method & execute the operation specified in the execute method.
If you don’t specify batch size inexecuteBatch()
method, 200 is the default size.