Saturday, 14 July 2012

Batchable Apex and Schedulable Apex

* It is a powerful new feature to do batch processing on our database records.
* It was released in summer'09.
* It is used to execute large volumes of data unlike ordinary DML and SOQL  statements.
* Tasks that require processing of large data volumes without any active human intervention can take advantage of this feature.
As an example, consider the task of validating addresses in your contacts when you can potentially have millions of contact records.  A batch job would be ideal for this scenario since you can start the batch job, continue to work or even log off while the job continues to execute.
* Batch-apex can execute large volumes of data up to 50 million records.
*To remove custom object records in bulk we need to delete it through data loader, now we can do it with Batch-apex.
*For Mass updating, inserting and other similar scenarios like this can be more conveniently handled with's Batch Apex.
* With Batch Apex, we can now build complex, long-running processes on the platform. This feature is very useful for time to time data cleansing, archiving or data quality improvement operations.
* To use this functionality, you need to implement the Database.Batchable interface.
* 'Database' is a class and 'Batchable' is the interface inside the class.
* This interface demands for three methods to be implemented:
   1. Start()
   2. execute()
   3. finish()
1. Start() Method:
* Start() method is called at the beginning of a batch Apex job. 
* Use this method to collect the records (of objects) to be passed to the "execute" method for processing. 
* The start() method determines the set of records that will be processed by the executeBatch method. We would need to construct a SOQL query and return a QueryLocator object.
return Database.getQueryLocator( 'SELECT Name, MailingAddress FROM Contact');
*QueryLocator can retrieve up to 50 million records at a time. 
*Instead of QueryLocator, we can use 'Iterable' which can process up to 50 thousand records.
* The Apex engine automatically breaks the massive numbers of records we selected into smaller batches and repeatedly calls the "execute" method until all records are processed.
* Start() and finish() methods executes only one time. 
* But execute() method execute many times based on number of batches.

2. execute() Method:
*  It will fetch the records from the start method.
* We can perform all the DML operations here on bulk volumes of data.

3. finish() Method:
* All the post processing operations like sending emails will be done here. 
* It is the interface which holds the run-time information.
Program to give 20% discount on all the books

//To provide 20% discount for all the books
/*BatchApexProcessing b = new BatchApexProcessing();
  Id jobid = Database.executeBatch(b,3);*/
  //Note: If there are any triggers for the object which we are using for the batchApexProcessing, it won't work
//For the below class we are using list, so it can accept upto 50000 records
//Database is the class name and Batchable is the interface name which is inside database class
//To access interface Database.Batchable we have to use
global class BatchApexProcessing implements Database.Batchable<Book__c> {
    global Iterable<Book__c> start(Database.BatchableContext bc){
        List <Book__c> bl = new List <Book__c>();
        bl = [select Id,Name,Author__c,Price__c from Book__c];
        return bl;
    global void execute(Database.BatchableContext bc, LIST<Book__c> bl) {
        for(Book__c b:bl) {
            b.Price__c *= 0.80;
        update bl;
    global void finish(Database.BatchableContext bc) {

//    ********    Using Query Locater    ****************

//For the below class we are using Query Locater, so it can accept upto 50 million records
global class BatchApexProcessing implements Database.Batchable<sObject>,Database.stateful { 
    //database.stateful is used to make data members with new values assigned should reflect
    String myname = 'SRINU';   
    global Database.QueryLocator <Book__c> start(Database.BatchableContext bc) {
        myname = 'SRINIVAS';
        System.Debug('***** My Name in Start *****'+myname);
        List <Book__c> bl = new List <Book__c>();
        String Query = 'select Id,Name,Author__c,Price__c from Book__C';
        return Database.getQueryLocator(Query);
    global void execute(Database.BatchableContext bc, LIST<book__c> bl){
        for(Book__c b:bl) {
            System.Debug('***** My Name in Start *****'+myname);
            b.Price__c *= 0.75;
        update bl;
    global void finish(Database.BatchableContext bc) {
        System.Debug('***** My Name in Start *****'+myname);


No comments:

Post a Comment