Achieving Salesforce Apex Bulkification In Three Simple Steps

As you may be aware, Salesforce imposes strict governor limits that can lead to unexpected failures. One of the most common governor limits is the “Too many SOQL Queries” restriction. Additionally, developers often face challenges because Triggers may not execute on just one record but could involve up to 200 records at once.

The main culprits for hitting these limits are loops and unoptimized queries. In this blog post, I will demonstrate how to bulkify a method in five straightforward steps. We will utilize collections and restructure the logic slightly to make almost any method suitable for bulk operations.

Step 1: Extract Queries from Loops

Every Salesforce developer must understand the importance of not running a query within a loop. In fact, regardless of the platform or programming language, it’s considered a best practice to avoid such actions.

Consider the following example of a query within a loop:

Rather than executing the query for Opportunities within the loop, it’s more efficient to perform it before the loop and utilize collections to obtain the IDs.

Executing the query just once and utilizing improved data structures will enhance the performance of our code. Improved performance translates to faster data processing, leading to an enhanced user experience

Step 2: Establish a Collection

In coding, a collection refers to an object that consolidates multiple elements into a unified entity. They are employed for efficient storage, retrieval, and manipulation of data. In Salesforce Apex, there are several types of collections, including List, Set, and Map.


Lists are the most straightforward type of collection to create, and if you have written any SOQL queries, you are likely already familiar with them. By default, SOQL queries return a list.


Lists and Sets exhibit distinct characteristics. The primary disparity between Sets and Lists is that Sets are unordered and contain unique elements. To access elements within a Set, one must iterate through all the items.

I typically employ Sets when I need to store a collection of IDs or strings where duplicate entries are possible and need to be managed.


A Map is a data structure that enables rapid indexing of individual elements, comprising a key and corresponding data values. Each entry in a Map consists of a unique key paired with its value. While the key values can be any primitive data type, they are commonly strings or IDs.

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.

Please don’t hesitate to leave a comment on this post or reach out to me if you have any inquiries.