Salesforce Apex Bulkification In 3 Easy Steps

Sharing is Caring

As you may recall, in Salesforce, there are some fairly stringent governor limits that can cause things to unexpectedly fail. One of the easiest and most common Governor Limits is the “Too many SOQL Queries” limit. And of course, many developers struggle with the fact that Triggers do not necessarily execute on only one record and could be related to 200 records at a time.

Loops and unoptimized queries are often the cause of hitting the limit. In this blog post, I will show you how to bulk a method in five very easy steps. We will take advantage of collections, and move the logic around slightly to make just about any method bulkified.

Step 1: Move Your Queries out of any loops

Every Salesforce developer has to learn at least once that you shouldn’t execute a query while in a loop. And, when you think about it, whenever you are writing code regardless of the platform or language it’s a really good practice to avoid doing that.

Here’s an example of a query in a loop

Instead of doing the query for Opportunities in the loop, we should do it before the loop and better use collections to get the ids.

By doing the query only once, and by using better data structures our code will be more performant. Better performance means our data processing should run faster resulting in a better user experience.

Step 2: Create a Collection

In programming, a collection is an object that groups multiple elements into a single unit. They are used for more efficient storage, retrieval, and manipulation. In Apex, there are a few different types of collections: List, Set, and Map.

Lists

Lists are the easiest collection to create and you’re probably already aware of them if you have written any SOQL. SOQL by default returns a list.

If you want to learn more about converting Lists to Sets or Sets to Lists check out my blog post “Converting Lists to Sets and Sets to Lists in Apex”.

Sets

Lists and Sets are very different from one another. The fundamental differences between sets and lists is that a Set is unordered and unique. To access elements in a set, you must iterate through all of the items.

I don’t generally use Sets unless I’m using it for storing a bunch of Ids or some strings that I need to work with where there’s a potential for duplicates.

Maps

A Map is of data structure that allows us to really quickly index individual elements – it consists of a key and the data values. Each Map entry contains exactly one unique key and it’s value. The key values can be any primitive value but are primarily a string or Id.

Maps are my favourite way to store data because it makes it much easier to work with related objects. It’s also possible to use Lists as the value if you need to work with more specialized items.

Step 3: Process Your Data Elements

When we are processing elements we should always make sure that we always handle the chance that any method could be called made times in a row. To do this, we should have all static methods actually accept a List, Set or Map so it can run in bulk and be far more efficient.

The easiest way to handle this is to make the parameters a List and have it return a List or a Map. The caller of a method can easily convert their calls to use a List in a few easy characters.

Summing it up

In this blog post, you have learned three easy steps to bulkify any method in Apex. Here’s an example of how not to do a createNewCallTask method.

This is a much better and safer way of doing the exact same thing.

Feel free to comment on this post or contact me if you have any questions.


Also published on Medium.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.