When working with Salesforce, there are times when you need to update a large volume of records. Doing this manually can be time-consuming and prone to errors. Using anonymous Apex directly quickly runs into governor limits.
Fortunately, Salesforce provides tools like Batch Jobs to help you handle bulk updates efficiently, ensuring you stay within governor limits and avoid performance issues.
One of the most common ways to perform bulk updates is by using the Database.Batchable
interface in Apex. This allows you to break the update process into smaller chunks, processing records in batches.
Each batch processes a defined number of records (e.g., 200 records per batch), reducing the risk of hitting limits like the total number of records updated in a single transaction.
Here’s how you can use Apex to update records in bulk:
Create a Batch Class: You define a batch class by implementing
Database.Batchable<SObject>
. Thestart
method selects the records, theexecute
method updates the records, and thefinish
method can be used for any post-processing tasks.Run the Batch in Anonymous Apex: After creating your batch class, you can execute it in the Salesforce Developer Console using Anonymous Apex. This runs the batch asynchronously, so it doesn’t block the user interface.
Example code for initiating a batch that updates 200 records at a time:
Database.executeBatch(new YourBatchClass(), 200);
Here’s a sample batch job for updating Account
records:
public class AccountUpdateBatch implements Database.Batchable<SObject> {
// Start method that defines the query to select Accounts
// Add any filters here to the query.
public Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('SELECT Id, Name FROM Account WHERE Name != null');
}
// Execute method that processes each batch of records
// Modify this to alter the Account records as desired
public void execute(Database.BatchableContext BC, List<Account> scope) {
for (Account acc : scope) {
acc.Name = acc.Name + ' - Updated'; // Example: appending "- Updated" to the Account Name
}
update scope;
}
// Finish method to handle post-processing if necessary
public void finish(Database.BatchableContext BC) {
System.debug('Batch Process Completed');
}
}
Note: Salesforce will not allow you to add or remove Apex directly on production. You’ll have to install and remove this code using CLI tools, change sets, or some other deployment method. It may also affect your test coverage, which would prevent deployment.
Using this approach ensures that you can handle large updates efficiently while respecting Salesforce's limits and ensuring minimal impact on system performance.
It’s a good practice to test this on a full sandbox and to make data backups before running it on production. When manipulating large volumes of data in this way there is the possibility that you can make a mess of your data.
You should also inspect your organization and understand the implications of updating these records - are there triggers or flows that will run?
As always, code responsibly.