Stellaxius Knowledge Center
  • Home
  • About Us
    • Our Team
    • Our Responsibility
      • Equality
    • Certifications
  • Services
    • Salesforce Consulting and Implementation
    • Salesforce Assessment
    • Software Development and Systems Integration
    • Process and Business Analysis
    • Program and Project Management
    • MuleSoft Integration
    • Data & Analytics
  • Success Stories
  • Careers
  • Blog
Contact us
Blog Home
Blog Categories:
  • Salesforce
    • CPQ
    • Net Zero Cloud
    • Release Notes
    • Sales Cloud
    • Salesforce Certifications
    • Service Cloud
  • Analytics &
    AI
    • Data Analysis
    • Einstein
  • Business Analysis &
    Implementation
  • Integration &
    Mulesoft
  • Marketing
    • Account Engagement
      (Pardot)
    • Marketing Automation
  • People &
    Culture
    • Human resources
    • Social Responsibility
  • Development
No Result
View All Result
Blog Home
Blog Categories:
  • Salesforce
    • CPQ
    • Net Zero Cloud
    • Release Notes
    • Sales Cloud
    • Salesforce Certifications
    • Service Cloud
  • Analytics &
    AI
    • Data Analysis
    • Einstein
  • Business Analysis &
    Implementation
  • Integration &
    Mulesoft
  • Marketing
    • Account Engagement
      (Pardot)
    • Marketing Automation
  • People &
    Culture
    • Human resources
    • Social Responsibility
  • Development
No Result
View All Result
Stellaxius Knowledge Center
Home Development

Why and How to Bulkify your Code

Bulkifying your Apex code is the key to assure it runs properly and smoothly! When you’re using Salesforce, the best practice is to do it. Read this article to understand some of the errors you may find and how you correct them through bulkification.

André MonteirobyAndré Monteiro
22nd November 2019 - Updated on 27th February 2025
in Development
0
A A
0
How to bulkify your code
74
SHARES
7.4k
VIEWS
Share on LinkedInShare on XShare on FacebookShare on Whatsapp

Table of Contents

  • 1. Why should you bulkify your code?
  • 2. What are Governor Limits?
  • 3. Overcoming Governor Limits with Maps
  • 4. Errors faced with not bulkified code and how to correct them
  • 5. Bulkify your code today!

First of all, what does “bulkify your code” mean?

The term refers to the concept of making sure your code properly handles more than one record at a time. “Bulkify” meaning your code is in bulk. When you’re working with Salesforce, it’s better for you to bulkify your code. It will avoid any possible errors!

01
of 05
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.

02
of 05
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?

03
of 05
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.

illustration on "how to bulkify your code"

Now that you know what maps are, let’s see which are the most common errors and how to fix them!

04
of 05
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:

Too many SOQL queries

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.

Too many SOQL queries

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:

Too many DML statements

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.

Too many DML statements

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.

Apex CPU time limit exceeded

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.

Apex CPU time limit exceeded

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.

05
of 05
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 our Knowledge Center to get the latest articles straight to your inbox!

Share5Tweet19Share30Send
Previous Post

Salesforce: A Force beyond Sales

Next Post

Sneak peek into Dreamforce 2019

André Monteiro

André Monteiro

As a Salesforce developer, I am always ready to create new solutions for new ideas and to develop them! Discover the world of coding, APEX, and Salesforce with me!

Related Articles

Development

What is Business Process Model and Notation (BPMN)?

30th July 2024 - Updated on 28th February 2025
119
Top Salesforce Summer’21 release new for developers!
Development

The top features from Salesforce Summer’21 Release for developers!

31st May 2021 - Updated on 17th February 2025
1.6k
Hottest Winter ’21 features for developers
Development

The Hottest Winter’21 Features For Developers!

9th December 2020 - Updated on 13th March 2023
164
Next Post
Salesforce Dreamforce 2019

Sneak peek into Dreamforce 2019

Dreamforce’19: A keynote on Pardot

Dreamforce 2019: A keynote on Pardot

Stellaxius

At Stellaxius, we are focused on turning our customers into superheroes by offering you simple-to-use consulting services and implementing sustainable solutions that transform people through technology and knowledge. We believe success comes with strong human relations to help transform businesses, improve people’s lives and have an impact on our community.

Categories

  • Analytics & AI
  • Business Analysis & Implementation
  • Integration & MuleSoft
  • Marketing
  • People & Culture
  • Salesforce
  • Development

Subscribe to our Blog

  • Careers
  • Privacy Policy
  • Contact Us

© 2025 Stellaxius Knowledge Center. All rights reserved.

No Result
View All Result
  • Login
  • Sign Up
  • Salesforce
    • CPQ
    • Net Zero Cloud
    • Release Notes
    • Sales Cloud
    • Salesforce Certifications
    • Service Cloud
  • Analytics &
    AI
    • Data Analysis
    • Einstein
  • Business Analysis &
    Implementation
  • Integration &
    Mulesoft
  • Marketing
    • Account Engagement
      (Pardot)
    • Marketing Automation
  • People &
    Culture
    • Human resources
    • Social Responsibility
  • Development

© 2025 Stellaxius Knowledge Center. All rights reserved.

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.
All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Skip to content
Open toolbar Accessibility Tools

Accessibility Tools

  • Increase TextIncrease Text
  • Decrease TextDecrease Text
  • GrayscaleGrayscale
  • High ContrastHigh Contrast
  • Negative ContrastNegative Contrast
  • Light BackgroundLight Background
  • Links UnderlineLinks Underline
  • Readable FontReadable Font
  • Reset Reset
  • SitemapSitemap
  • FeedbackFeedback