When we want to deal with large number of records we go for batch apex. The code inside batch class runs asynchronously i.e in future context. The governor limit are also more as compared to synchronous code.
When we use batch apex we implements Database.batchable() interface and we have to include three methods and they are as follows:
1)start
2)execute
3)Finish
Start method and finish method are called only once inside batch class.
- Start method collects the data for processing.
- Execute method performs operations.
- The order of execution is not guaranteed in execute method.
- Finish method generally used for sending emails or calling another batch class when the current batch is completed.
Syntax:
global class batch implements Database.Batchable < sObject > {
global (Database.QueryLocator or Iterable<sObject>)
start(Database.BatchableContext bc) {
//query on object;
//return Database.getQueryLocator(query);
}
global void execute(Database.batchableContext bc, List < SObject > scope) {
//some processing.
}
global void finish(Database.BatchableContext bc) {
//job such as sending email or calling another batch class
}
}
No comments:
Post a Comment