In this blog we will learn how to use Batch Apex. It is used to run large jobs (think thousands or millions of records!) that would exceed normal processing limits. Each time you invoke a batch class, the job is placed on the Apex job queue and is executed as a discrete transaction.
Advantages:
- Every transaction starts with a new set of governor limits, making it easier to ensure that your code stays within the governor execution limits.
- If one batch fails to process successfully, all other successful batch transactions aren’t rolled back.
Key Points
- To write a Batch Apex class, your class must implement the Database.Batchable interface and include the following three methods:
- start()
- execute()
- finish()
- If your code accesses external objects and is used in batch Apex, use Iterable<sObject> instead of Database.QueryLocator.
- The default batch size is 200 record.
Apex Batch Class
Create an apex class which implements Database.Batchable interface and class must be globle like mentioned below.
global class batchExample implements Database.Batchable<sObject> {}
Now describe all batchable class method i.e.
global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) { // collect the batches of records or objects to be passed to execute } global void execute(Database.BatchableContext bc, List<P> records){ // process each batch of records } global void finish(Database.BatchableContext bc){ // execute any post-processing operations }
Update Account Name
Account Name before executing batch class:
Here is the Batch class to update the account name.
global class batchExample implements Database.Batchable<sObject> { /** * Webkul Software. * * @category Webkul * @author Webkul * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ global Database.QueryLocator start(Database.BatchableContext BC) { // collect the batches of records or objects to be passed to execute String query = 'SELECT Id,Name FROM Account'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Account> accList) { // process each batch of records for(Account acc : accList) { // Update the Account Name acc.Name = acc.Name + 'Webkul'; } try { // Update the Account Record update accList; } catch(Exception e) { System.debug(e); } } global void finish(Database.BatchableContext BC) { // execute any post-processing operations } }
Run BATCHABLE Class in Developer Console.
To invoke a batch class, simply instantiate it and then call Database.executeBatch with the instance:
batchExample be = new batchExample(); database.executeBatch(be);
You can also optionally pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch.
batchExample be = new batchExample(); database.executeBatch(be,100);
Account Name after executing batch class:
Support
That’s all for how to use Batch Apex in Salesforce, still have any issue feel free to contact us and let us know your views to make the code better https://wedgecommerce.com/contact-us/