Thursday 15 April 2021

Mass\Bulk Operations in Salesforce using Batch Apex


There was a requirement for Mass/bulk Update of  more than thousands of records on the particular object based on certain action on object for one of our clients - consulting firm based out of Atlanta, GA, USA.

We, all are familiar with updating records using Apex class but updating more than a thousand records or fire DML on thousands of rows on particular objects is very complex in Salesforce and it does not allow you to operate on more than a certain number of records which satisfy the Governor limits.

But for medium to large enterprises, it is essential to manage thousands of records every day. Adding/Updating/Deleting them when needed. Salesforce has come up with a powerful concept called Batch Apex. It allows you to handle a thousand number of records and manipulates them by using a specific syntax.

For updating the thousand of records we need to develop the Batch Apex class which can mass update the records on the particular object. As Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks of data.

We have developed a custom global apex class that extends Database.Batchable interface because Salesforce compiler will come to know, this class incorporates batch jobs. Below is a sample class that is designed to update all the records of Account object (Let's say your organization contains more than 50 thousand records and you want to mass update all of them). 

batch class
After using the code, we have to go to Developer Console and click on Debug and then Open Execute Anonymous Window & Enter the following code in the box and click on Execute to see the result.
Using Batch Apex, we can perform Adding/Updating/Deleting of thousands number of records for a particular object. As Batch Apex is asynchronous execution of Apex code, specially designed for processing a large number of records and has greater flexibility in governor limits than the synchronous code.

If you have any questions you can reach out our Salesforce Consulting team here.

No comments:

Post a Comment