First of all, let us start by checking what “bulkify your code” means.
The term refers to the concept of making sure your code properly handles more than one record at a time. When you’re working with Salesforce, it’s better for you to bulkify your code. It will avoid any possible errors!
Below, find the index for this piece of content:
- Why should you bulkify your code?
- What are governor limits?
- Overcoming governor limits with maps
- Errors faced with not bulkified code and how to correct them
- Bulkify your code today!
Why should you bulkify your code?
You should bulkify your code because it becomes especially important when importing or inserting more than one record, so that your code properly handles all the records in different contexts. That is why we advise you to bulkify all your Apex classes so as to improve your org performance, and not just the triggers, as suggested by Salesforce.
Apex code has Governor Limits (we will come to that later) and, for example, a Trigger can only process up to 200 records at once. Yet, it is rather common that with Data Loader you import much more than that. Thus, you have to be careful with the way that you build your Trigger so that you don’t face, for instance, the “Too many SOQL queries: 101” error.
What are Governor Limits?
Governor Limits are a set of rules that Salesforce has to improve performance in your system (by writing an efficient and scalable code) and to prevent your code from using all the shared resources. However, governor limits also present limitations, as you cannot improve your usable resources by upgrading your license or by any other way. Besides, you must be careful because Governor Limits are shared by all the triggers you have for one object, being either one or five triggers per object.
So, how can you overcome the Governor Limits drawbacks?
Overcoming Governor Limits with Maps
Maps are the solution to beat Governor Limits. So, what is a Map?
Basically, a Map is a searchable table composed of two fields. First, a unique value that you use to search, such as a record Id or a unique number or string. In the second one, you’ll find the information that you want like the record itself.
//instantiate the map Map<Integer,String> fruitMap = new Map<Integer,String>(); //add fruit to the map with identifier number //fruiMap.put(key,value); fruitMap.put(1, 'apple'); fruitMap.put(2, 'banana'); fruitMap.put(3, 'pineapple'); //get the fruit that i want using the get() function //with the key of the frui that i want String fruitThatIWant = fruitMap.get(1); /use System.assertEquals to confirm that i get the fruit that i want System.assertEquals('apple', FruitThatIWant);
So, having this fruit Map is the same as having the following table and search for the first column.
Now that you know what maps are, let’s see which are the most common errors and how to fix them!
Errors faced with not bulkified code and how to correct them
The next showing errors are faced when imported 200 records via Data Loader or Data Import Wizard.
Too many SOQL queries: 101
The most common error that it is faced with not bulkified code is “Too many SOQL queries: 101”.
To have this error is as simple as have a 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 get the Phone field from the respective account and, as it reaches the 100 SOQL queries limit, you will get the mentioned error. So, you should never use a SOQL query inside a loop.
To improve the above code so that you won’t face the SOQL error you should use the Map class we mentioned earlier to save all the Account information and search for it inside the Trigger.new for cycle.
This way you will not reach the SOQL limit because you only use one query in the whole trigger.
Too many DML statements: 151
Another error you sometimes may face is “Too many DML statements: 151”. In this error, the total number of DML issued is 150 and it could appear in the following case:
In this case, the error you face happens because of the insert inside the for cycle. As the Trigger.new list has 200 records, and it reach the limit, so, the solution is to create a list, add the new contacts to that list and then insert it.
By creating a list of contacts to insert, only one insert is needed.
Apex CPU time limit exceeded
You can also face the “Apex CPU time limit exceeded” error. This error means what is explained in the message: you reach the CPU usage limit that your org can use, as is explained in the governor limits above.
This error occurs in case you have a lot of contacts because in every new account inserted, the inner for cycle will run across all contacts in the list.
To upgrade your code, you should create a Set of the inserted Account IDs and use it to query only the contacts that belong to those Accounts. Hereafter, you can create a Contact map in a way to search by Phone inside the Trigger.new for cycle.
Bulkify your code today!
Well, now that you are aware of the impacts of not bulkifying your code, the errors it may carry, and how to correct them through bulkyfication, let’s get down to work! Use what you learn to build clean, strong, and efficient code to improve your organization’s performance.
Subscribe to our Knowledge Center to know more about software development and the world of Salesforce! Start to bulkify your code today.
SUBSCRIBE KNOWLEDGE CENTER
Subscribe for free to Knowledge Center's monthly digest to receive the hottest news and newest posts directly on your Inbox.