Author: Mounika Arrabothu, Senior Developer
What Is Bulkification?
Bulkification is either the process of combining repetitive tasks in Apex
— or —
Bulkification is the process of ensuring the Apex code properly handles more than one record at a time. When a batch of records initiate Apex, a single instance of that Apex code is executed, but that code must have the capacity to handle all the records in that given batch.
Why Is Bulkification Important?
- When performing DML calls in a trigger on each sObject, the sObject is apt to use resources inefficiently. If the trigger-context variable holds more than 150 records, sObject will exceed the 150 DML statements limit. Thus, it’s important to perform DML calls on a collection of sObjects whenever possible.
- In Salesforce, Trigger.new will batch up mass updates, including up to 200 records at once (as commonly happens when using tools like Data Loader). So, for example, if you do one SOQL query per record, you’ll exceed the 100 SOQL query limit.
- Bulkifying is key to assuring that Apex Code runs properly and smoothly.
Benefits of Bulkification
- Bulkified code is capable of processing a large number of records efficiently, and of running within governor limits on the Lightning Platform. These governor limits are in place to ensure that runaway code doesn’t monopolize resources on the multitenant platform.
- Bulkify Apex code in triggers that will increase the triggers’ performance, consume less server resources, and reduce the likelihood of exceeding platform limits.
Implementing Bulkification
Avoid SOQL Queries and DML statements inside FOR loops. Executing queries or DML operations within an iteration increases the risk that the governor limits will be exceeded. Instead, move any database operations (SOQL or DML) outside of the FOR loops. If you need to query, query once, retrieve all the necessary data in a single query, then iterate over the results. If you need to modify the data, follow these steps to batch it into a list and invoke your DML once on that list:
- Prepare a collection of the associated record type.
- Add each record to the collection.
- Once all the records have been added to the collection, perform the DML operation on the collection, or use the collection in Queries.
Possible Errors When Code Is Not Bulkified — and How to Correct Them
The following errors may occur when 200 records are imported via Data Loader or Data Import Wizard.
1. Too many SOQL queries: 101
The most common error faced when code is not Bulkified is “Too many SOQL queries: 101.”
This error can occur for reasons as simple as having an SOQL query inside a FOR loop.
For example:
In this case, if each imported contact has no phone number, the trigger will try to do 200 SOQL queries to retrieve the Phone field from the respective account and, as it reaches the 100 SOQL queries limit, will produce the aforementioned error. To avoid this, never use a SOQL query inside a loop.
To improve the above code, and avoid the SOQL error, use the Map class to save all the Account information and search for it inside the Trigger.new FOR cycle.
By only using one query in the whole trigger, you’ll never reach the SOQL limit.
2. Too many DML statements: 151
Another error you may come across is “Too many DML statements: 151.” Here, the limit for DML statements issued is 150. See the following example:
In this case, the error occurs because of the FOR cycle insert, and the Trigger.new list exceeds the limit with 200 records. The solution, then, is to create a list, add the new contacts to that list, and then insert it as such:
By creating a list of contacts to insert, only one insert is needed.
3. Apex CPU time limit exceeded.
You may also face the “Apex CPU time limit exceeded” error, which tells you that you’ve reached your organization’s CPU usage limit.
This error occurs when you try to insert a new account, causing the inner FOR cycle to run across all the contacts in the list, and you have too many contacts.
To upgrade your code, create a set of the inserted Account IDs, and use it to query only the contacts that belong to those accounts. Then you can create a Contact map inside the Trigger. new FOR cycle to search by Phone.
Conclusion
Bulkification is a relatively simple concept that’s not hard to implement. However, ignoring Bulkification can have catastrophic results, including that governor limits can be easily missed during testing or while initially populating a growing environment.
Ignoring bulkification best practices in Salesforce will necessitate redoing processes later on, after they’re established and too complicated to change. Needless to say, learning Bulkification as early as possible will pay dividends.