BATCH APEX IN SALESFORCE
Batch as the name suggests, is used when a large data (bulk) volume is involved and it has to be redundantly processed using a particular logic.
The Batch apex, can be used to conveniently perform time to time task and some real complex job ranging from data cleansing, archiving the data to the other quality improvements.
When to use Batch Apex
The typical use of batch apex is when you want to do a bulk job, but if you do it in a regular apex you’re bound to hit the governor limits.
Batch Classes process the set of records you pass to it in batches of maximum 200 per batch.
Advantages of using Batch Apex
- Higher Governor Limits
- Can be used to process in batches
- Can be scheduled to run at different time. (read more)
- Work around to other governor limits
Here is the governor limit difference in using batch
Area Normal Context BatchContext
1.SOQL queries 100 SOQL per cycle 200 SOQL per cycle
2.records SOQL queries 50,000 50,000,000 (getQueryLocator)
3.code statements 200,000 1,000,000 (Winter '14)
4. Heap Size 6 MB 12 MB
global class ExampleBatchClass implements Database.Batchable<sObject>{
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<sObject>scope){// Logic to be Executed batch wise
}
global void finish(Database.BatchableContext BC){// Logic to be Executed at finish}
}
ExampleBatchClass b = new ExampleBatchClass();
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,10);
Note : if batch size is not mentioned it is 200 by default.
Note : Database.QueryLocator (If you use a QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed up to 50 million records)
No comments:
Post a Comment