Play Games

Search This Blog

Saturday, December 7, 2013

Batch processing

Batch Processing


why is it used?

 When there are large number of records(for ex thousands of records) to

process at a time,there may be problem of hitting governor limits.So,to avoid

this,we use batch processing in salesforce.

Advantage:

1)A large number of records can be processed.

2) Result of one batch cannot affect other batch.It means every batch is descrite.

How to use:

In order to use batch processing, first we need to create a class that implements

Database.Batchable<sObject> interface, then call that class in visualforce page.

Database.Batchable<sObject> interface has 3 methods

1) Global (Database.QueryLocator | Iterable<subject>)

 start(Database.BatchableContext bc){}

2)global void execute(Database.BatchableContext bc,list<p>){}

3) global void finish(Database.BatchableContext bc){}

The start() is used to get the records to be processed which are then passed as

parameter to execute().the processing is done in execute().

Finish() is used to send confirmation mails.

For example below is an example of processing all contacts and updating one

custom field in it to some value.

First is a class implementing the Database.Batchable<sObject> interface.

global class ContactReassignment implements Database.Batchable<sObject> {

 String query;

 String email;

 global Database.querylocator start(Database.BatchableContext BC) {

 query='Select id ,Rich_Text__c from Contact ';

 system.debug('QUERY'+query);

 system.debug('hai');

 return Database.getQueryLocator(query);

 }

 global void execute(Database.BatchableContext BC, List<sObject> scope) {

 system.debug('hai');

 system.debug('QUERY'+query);

 List<contact> accns = new List<contact>();

 for(sObject s : scope) {

 contact a = (contact)s;

 a.Rich_Text__c='testing';

 accns.add(a);

 }

 update accns;

 system.debug('#############3'+accns);

 }

 global void finish(Database.BatchableContext BC) {

 system.debug('hai');

 AsyncApexJob a = [SELECT Id, Status, NumberOfErrors,

JobItemsProcessed,

 TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id

=:BC.getJobId()];

 system.debug('ksk'+a);

 String UserEmail = 'Any EMAIL';

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

 mail.setToAddresses(new String[] {UserEmail});

 mail.setReplyTo('ANY EMAIL');

 mail.setSenderDisplayName('Batch Processing');

 mail.setSubject('Batch Process Completed');

 mail.setPlainTextBody('Batch Process has completed');

 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 }

 Public Pagereference Updatecon() {

 ContactReassignment ACB = new ContactReassignment();

 Database.executeBatch(ACB);

 system.debug('***************'+ACB);

 return null;

 }

Next is the page calling that class.

<apex:page controller="ContactReassignment" action="{!Updatecon}">

 batch processing successful
</apex:page>


we can also schedule batch apex.
In order to schedule batch class,we need to write schedulable batch class

global class ContactReassignmentBatchSchedular implements schedulable
{
    global void execute(SchedulableContext sc)
    {
    ContactReassignment b = new ContactReassignment (); //ur batch class
     
        database.executebatch(b);
   }   
    
}


If u want to specify batch size,we can specify  in executebatch() as below
database.executebatch(b,100);

Once u wrote schedulable batch class,u need to schedule it as follows :

step 1:
Go to Setup--> develop--> apex classes
step 2:

click on  schedule   apex

step 3:

Here U can select schedulable batch class,date and time.
Thats it.Based on ur selection,the schedul apex runs..




No comments:

Post a Comment