Batch Class

Author - Webner
9.08.2021
|
0 Comments
||

Batch class in salesforce 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.

For eg – Consider a situation wherein you have to process large amounts of data on a daily basis and have to delete some unused data. It would be difficult to manually do so. This is where batching with Apex Salesforce comes to your rescue.

Batch class in salesforce is specifically designed to handle bulk data and have greater governor limits.

How to use Batch class?

To use batch class, the Batchable interface provided by Salesforce needs to be implemented. It has three methods :

  1. Start – This method is called at the starting of batch call to collect data on which the batch job is operating. It breaks the data into batches. The ‘QueryLocator’ method is used to operate with the simple SOQL query to generate the scope of objects inside a batch job.
  2. Execute – It is called after the start method. It actually executes the batch.
  3. Finish – This method will be called at last. It is basically responsible to perform post-processing operations like sending an email. When this process is called all batches are already executed.

Example of Batch Class

Following is an example to update all the Contacts.
// Batch Job for Processing the Records
global class batchContactUpdate implements Database.Batchable<sObject>{
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id,Name FROM Contact;
return Database.getQueryLocator(query);
}
// Execute method
global void execute(Database.BatchableContext BC, List<Account> scope) {
for(Contact a : scope)
{a.Name = a.Name + 'Updated';
}
update scope;
}
// Finish Method
global void finish(Database.BatchableContext BC) {
}
}

Execution of Batch Class

The basic syntax is:
Id <variable_name> = new <variable_name>;
database.ExecuteBatch(new <class_name>(), batch_size);

You can use the following code in developer console anonymous window or in any other apex class or trigger to run the above batch class.
batchContactUpdate b = new batchContactUpdate();
database.executeBatch(b);

Advantages of Batch Apex in Salesforce

  • Whenever a transaction is executed, Batch Apex ensures that the code stays within the governor limit.
  • Until a batch is not successfully executed, Batch Apex won’t execute the following batches.
  • A large set of records can be processed together on a regular basis using Batch Apex classes.
  • The interface can be scheduled to run batches at different time periods.
  • Asynchronous operations can be implemented by Batch Apex classes.
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *