Thursday 3 April 2014

Batch Apex and Scheduling Batch Apex in Salesforce


Batch Apex in Salesforce
To use batch Apex, you must write an Apex class that implements the Salesforce-provided interface Database.Batchable, and then invoke the class programmatically.

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.

Syntax: global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}

 This method returns either a Database.QueryLocator object or an iterable that contains the records or objects being passed into the job.

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.

Syntax: global void execute(Database.BatchableContext BC, list<P>){}

This method takes the following:
o    A reference to the Database.BatchableContext object.
o    A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, the returned list should be used.
Batches of records execute in the order they are received from the start method.

Finish Method

Syntax: global void finish(Database.BatchableContext BC){}

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.

Example of Batch Apex Class: 

Batch Schedule Class

  1. global class batchContactUpdate implements Database.Batchable<sObject>
  2. {
  3.     global Database.QueryLocator start(Database.BatchableContext BC)
  4.     {
  5.         String query = 'SELECT Id, FirstName,LastName FROM Contact';
  6.         return Database.getQueryLocator(query);
  7.     }
  8.  
  9.     global void execute(Database.BatchableContext BC, List<Contact> scope)
  10.     {
  11.          for(Contact a : scope)
  12.          {
  13.              a.FirstName=a.FirstName+'FirstName is Updated';
  14.              a.LastName = a.LastName +'LastName is updated';          
  15.          }
  16.          update scope;
  17.     }  
  18.     global void finish(Database.BatchableContext BC)
  19.     {
  20.     }
  21. }

Schedule Class

  1. global class BatchScheduleUpdate implements Schedulable
  2. {
  3.     global void execute(SchedulableContext sc)
  4.     {
  5.         // Implement any logic to be scheduled
  6.        
  7.         // We now call the batch class to be scheduled
  8.         BatchContactUpdate b = new BatchContactUpdate ();
  9.        
  10.         //Parameters of ExecuteBatch(context,BatchSize)
  11.         database.executebatch(b,200);
  12.     }
  13.    
  14. }

Schedule from Developer Console

  1. BatchScheduleUpdate batchSch=new BatchScheduleUpdate();
  2. String sch='0 5 2 * * ?';
  3. //System.schedule(String jobName, String cronExp, APEX_OBJECT schedulable);
  4. System.schedule('Batch Schedule', sch , batchSch);

Hope this will helpful for you...!
Thank you...!

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...