Batch Apex
------------------------------------------------------------------------------------------------------------------------Batch Apex is used to run large jobs (think thousands or millions of records!) that would exceed normal processing limits. Using Batch Apex, you can process records asynchronously in batches (hence the name, “Batch Apex”) to stay within platform limits. If you have a lot of records to process, for example, data cleansing or archiving, Batch Apex is probably your best solution.
Start method:
The start method is called at the beginning of a batch Apex job. Use the start method to collect the records or objects to be passed to the interface method execute.
Execute Method:
The execute method is called for each batch of records passed to the method. Use this method to do all required processing for each chunk of data.
Finish Method
The finish method is called after all batches are processed. Use this method to send confirmation emails or execute post-processing operations.
Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each.
The Apex governor limits are reset for each transaction. If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back.
--------------------------------------------------------------------------------------------------------------------------
Useful Links:
1. http://www.infallibletechie.com/2012/05/apex-scheduler.html
2. https://trailhead.salesforce.com/en/modules/asynchronous_apex/units/async_apex_batch
3. http://blog.shivanathd.com/2013/01/how-to-write-batch-class-in.html
-------------------------------------------------------------------------------------------------------------------------
global class BatchAccountUpdate implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Account a : scope)
{
a.Name = a.Name+'Salesforce King';
}
update scope;
}
global void finish(Database.BatchableContext BC) {
}
}
--------------------------------------------------------------------------------------------------------------------------
global class BatchScheduleAccount {
global void execute(SchedulableContext sc)
{
// Implement any logic to be scheduled
// We now call the batch class to be scheduled
BatchAccountUpdate b = new BatchAccountUpdate ();
//Parameters of ExecuteBatch(context,BatchSize)
database.executebatch(b,200);
}
}
------------------------------------------------------------------------------------------------------------------------
Test class for Batch Apex:
@isTest
public class BatchAccountUpdate_Test {
public static testmethod void batchMethod(){
List<Account> aclist=new List<Account>();
for(integer i=0;i<=200;i++){
Account ac=new Account();
ac.name='TEST'+i;
aclist.add(ac);
}
insert aclist;
Test.startTest();
BatchAccountUpdate b=new BatchAccountUpdate();
// database.executeBatch(b);
Database.QueryLocator ql = b.start(null);
b.execute(null,aclist);
b.Finish(null);
Test.stopTest();
}
}
--------------------------------------------------------------------------------------------------------------------------
Test class for scheduler class:
@isTest
public class BatchScheduleAccount_Test {
public static testmethod void batchMethod(){
test.startTest();
BatchScheduleAccount ac=new BatchScheduleAccount();
ac.execute(null);
test.stopTest();
}
}
------------------------------------------------------------------------------------------------------------------------
Example of the coding is very good SalesForce Online Training
ReplyDeleteThanks
ReplyDelete