1.
Write a schedulable class for your Batch class.
·
Lets us assume my batch class name is SFDC_ExecuteAccountBatch.
·
To schedule it we need to create a new class and
implement a schedulable interface
·
Syntax:
global with sharing class SFDC_ExecuteAccountBatchScheduler implements Schedulable
{
global void
execute(SchedulableContext sc) {
SFDC_ExecuteAccountBatch
b = new SFDC_ExecuteAccountBatch();
database.executebatch(b);
database.executebatch(b);
}
}
2.
Write a batch class with Schedulable interface
·
To avoid writing extra class we can implement a schedulable interface in batch class to make it easy and maintainable.
·
Syntax:
global class SFDC_ExecuteAccountBatch implements Database.Batchable<SObject>,
Schedulable {
global
Database.QueryLocator start(Database.BatchableContext BC) {
//
query on required object and return
Database.QueryLocator
}
global
void execute(Database.BatchableContext BC,
List<Sobject> scope) { // execute method
// for batch class
// do you
processing here
}
global
void execute(SchedulableContext sc){ // execute method for
//
schedulable interface
//execute
your batch class here
` SFDC_ExecuteAccountBatch b =
new SFDC_ExecuteAccountBatch();
database.executebatch(b);
database.executebatch(b);
}
global
void finish(Database.BatchableContext BC) {
//
do completion activity like sending email
}
}
Important Batch Apex Governor Limits
- Up to 5 batch jobs can be queued or active concurrently.
- Up to 100 Holding batch jobs can be held in the Apex flex queue.
- In a running test, you can submit a maximum of 5 batch jobs.
Best Practice
- Use extreme care if you are planning to invoke a batch job from a trigger. You must be able to guarantee that the trigger does not add more batch jobs than the limit.
- Use Database.Stateful with the class definition if you want to share instance member variables or data across job transactions.
- Methods declared as future aren’t allowed in classes that implement the Database.Batchable interface.
- Methods declared as future can’t be called from a batch Apex class.
- All methods in the class must be defined as global or public.

Comments
Post a Comment