This is a brief introduction to batch processing
Batch Requirements
Every batch class requires 4 things. To implement Database.Batchable<sObject>, have a start(),execute() and finish() methods.
global class UpdatetFields implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,
List<sObject> scope){
}
global void finish(Database.BatchableContext BC){
}
}
Governor Limits
In total 50 million records can be processed by a batch class and 5 batches can be run simultaneously, giving a total of 250 million records that can be processed at 1 time.
Remember batch classes still need to adhere to governor limits, so if any soql’s are performed do remember that these soql’s need to adhere to governor limits
The following is likely to break governor limits because List<sObject> scope is likely to contain more than 200 records and so the soql in bold will run 200 times per batch that is processed.
global void execute(Database.BatchableContext BC,
List<sObject> scope){
ID thisCustID;
List<Customers__c> allsubs;
for (SObject sub: scope){
thisCustID = (ID)sub.get(‘Customers__c');
allsubs = [Select Name From Customers__c where id =: thisCustID];
}
}
Types of Batch Classes
Using Database.QueryLocator this passes an list of SObjects. This is the most common type used in batch processing. Or you can use Iterable which allows you to step through the records more easily, which is mainly used for more complex soql’s such as nested soql’s.
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
Or
global Iterable start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
And
If your batch performs a callout you must implement Database.AllowsCallouts
global class SearchAndReplace implements Database.Batchable<sObject>,
Database.AllowsCallouts{
}
And
If you specify Database.Stateful in the class definition, you can maintain state across these transactions. This is can be used for example if you want to make some kind of summary, or total of the records processed, or could be used to summarise all the successful or failure transactions produced etc.
global class UpdatetFields implements Database.Batchable<sObject>, Database.Stateful{
}
Ensure Batches Don’t Try To Update The Same Records
Use AsyncApexJob to query when a batch has completed processing so you can run the next batch
ID batchprocessid = Database.executeBatch(reassign);
AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors
FROM AsyncApexJob WHERE ID =: batchprocessid ];
If (aaj.Status == ‘Completed’){
//run next batch
}
Else { }
No comments:
Post a Comment
Note: only a member of this blog may post a comment.