Batch Apex in salesforce Explained

Batch Apex in salesforce

Batch Apex in salesforce is exposed as an interface that must be implemented.Batch jobs can be programmatically invoked at runtime using Apex.

You can only have five queued or active batch jobs at one time.
The Database.Batchable interface contains three methods namely start, execute and finish methods that must be implemented.
Start method :-
You need to call start method at the beginning of Batch apex to collect records or objects and to pass them to execute interface method, start method returns either Database.QueryLocator object or an iterable that contains the records or objects
global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) 

{

/* logic goes here */

}
Execute method :-
Collection of records from start method will be chunked and processed in execute method, DML operations can be performed on execute method.
global void execute(Database.BatchableContext BC, list<P>)

{

/* logic goes here */

}
Finish method :-
This method is the final one in Batch apex interface which will be called once when all the data’s are processed in execute method.
global void finish(Database.BatchableContext BC)

{

/* logic goes here */

}
To send confirmation emails or execute post-processing operations use this method.
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

 

 

Be the first to comment

Leave a Reply

Your email address will not be published.


*