Hello folks, Here we will see what is Batch Apex in Salesforce? Batch Apex allows you to execute a piece of apex code asynchronously. It is basically used for processing large data and provide greater flexibility over governer limit than the synchronous code.
After completing this tutorial you will be able to understand:
- What is Batch Apex in Salesforce?
- Why we need Batch Apex?
- How does Batch Apex work?
- How to use Batch Apex?
What you’ll do?
- Create a simple Batch Apex class that will simply update Account Records.
What is Batch Apex in Salesforce?
Batch Apex allows you to execute a piece of apex code asynchronously. It is basically used for processing large data and provide greater flexibility over governer limit than the synchronous code.
Why we need Batch Apex?
Suppose I want to perform field update on all the record of Standard Account object in salesforce organization, then governor limits would restrict from field updating of every record. Because in a single transaction we can update only 10000 records. So if you have more than 10000 records to update you can use Batch apex.
How does Batch Apex work?
Batch apex does not perform an update operation on all the records instead it divides them into no of subtasks. Batch apex will fetch all the records that need to update and divide them into a batch of 200 records. The update operation is performed on every batch of 200 records separately.
How to use Batch Apex?
To implement Batch apex we need to implement Database.Batchable interface.
// Batch Job for Processing the Records global class ExampleBatch implements Database.Batchable<sobject> { // Start Method global Database.Querylocator start (Database.BatchableContext BC) { } // Execute method global void execute (Database.BatchableContext BC, List<sobject> scope) { } // Finish Method global void finish(Database.BatchableContext BC) { } } The Database.Batchable interface provides three methods:
Start.
Execute
Finish
Start:
This is the first method that will call automatically. This method collects the records or objects on which the operation should perform and divide them into subtask and transfer to execute method.
Execute:
This method performs the operation (for example updating records) on the records fetched from the start method.
Finish:
The finish method is called in the last when all batches are processed. This method can be used to send confirmation email notification.
Example:
Here I am creating a simple example of Batch Class that will simply update Account Name of Account Object.
global class batchAccountUpdate implements Database.Batchable<sobject> { global Database.QueryLocator start(Database.BatchableContext bc){ String query = 'SELECT Id, Name FROM Account'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext bc, List<account> scope) { for(Account a : scope) { a.Name = a.Name + 'Updated'; } update scope; } global void finish(Database.BatchableContext bc){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new String[] {'youremal@domain.com'}); mail.setReplyTo('youremal@domain.com'); mail.setSenderDisplayName('Account Batch Processing'); mail.setSubject('Account Batch Process Completed'); mail.setPlainTextBody('Account Batch Process has completed'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }
Scheduling Batch Apex:
Schedule a Batch apex you need to implement a Schedulable interface which provides an execute method.
Global class batchAccountUpdateSchedular implements Schedulable{ Global void execute(SchedulableContext sc){ batchAccountUpdate b= new batchAccountUpdate(); Database.executebatch(b); } }
Now you can Schedule this apex class by following the below steps:
Step 1: Goto Setup>Apex Classes.
Step 2: Now Click on Schedule Apex.
Step 3: Prvoide all the details like name, class to schedule, start time and frequency and Save. Here we done.
View More:
- Map Class in Apex Salesforce.
- Standard Controllers in Salesforce.
- How to Implement Pagination in Lightning Component Salesforce.
- Display Record List in Lightning Component Salesforce
Conclusion:
I hope you liked this post. Please don’t hesitate to comment for any technical help. Your feedback and comments are always appreciated.
Thank You.
online pharmacy canada discount pharmacy canadian pharmacy cialis
discount rx my canadian pharmacy pharmacy rx one
california pharmacy online pharmacy drugstore global pharmacy canada
the peoples pharmacy 24 hour pharmacy near me pharmacy in canada
ed medications best online pharmacy legit online pharmacy
pharma on line pharmacy nearest drugstore
best drugstore primer ed meds online the pharmacy
My programmer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year and am anxious about switching to another platform. I have heard good things about blogengine.net. Is there a way I can import all my wordpress posts into it? Any help would be really appreciated!|