0% found this document useful (0 votes)
6 views3 pages

Understanding Code Bulkification in Apex

Bulkifying code is important to avoid hitting governor limits when triggers fire on large numbers of records. Triggers can include up to 200 records at once, and share the same governor limits regardless of the number of triggers firing. Not bulkifying code means repetitive DML statements or SOQL queries will likely exceed limits and cause errors. Properly bulkified code uses lists to perform all DML or SOQL outside of loops to avoid these limits.

Uploaded by

ChinmayaTeella
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Understanding Code Bulkification in Apex

Bulkifying code is important to avoid hitting governor limits when triggers fire on large numbers of records. Triggers can include up to 200 records at once, and share the same governor limits regardless of the number of triggers firing. Not bulkifying code means repetitive DML statements or SOQL queries will likely exceed limits and cause errors. Properly bulkified code uses lists to perform all DML or SOQL outside of loops to avoid these limits.

Uploaded by

ChinmayaTeella
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Preface: this post is part of the Bulkify Your Code series.

Youll often hear Apex developers say bulkify your code!


To bulkify your code means to combine repetitive tasks in Apex! Its the only
way to get aroundGovernor Limits especially our #1 most important limit!
Before I give you an example, its important to know exactly why you need to
bulkify:
Up to 200 records can enter your trigger at once!
Remember [Link]? Salesforce will batch up mass updates and
include up to 200 records at once in it (this commonly happens when using
tools like Data Loader). So for example, if you do one SOQL query per
record, youre going to go over the 100 SOQL query limit!
Triggers are on the same shared Governor Limit!
Got ten triggers that run when an Account is updated? Each trigger does
not get its own set of limits! One limit per update, no matter how many
triggers are run! This means that if each of your ten triggers runs 12 SOQL
queries, you will run a total of 120 SOQL queries and go over the100
SOQL query limit! This means you need to be extra conservative!
You will be tested on bulkification during your job interviews
You cant have code in a production org thats not bulkified because your

users will get errors! Thats why every job interview specifically tests for
this knowledge. The good news is that 90% of people I interview do not
understand how to bulkify, so this is your chance to stand out!
Bulkification Example: Lets analyze the governor limit for DML statements.
This code is not bulkified and will go over the 150 DML statements limit:

//Rememberthatupto200recordscanbein
[Link]
for(Opportunityopp:[Link]){
Taskt=newTask();
[Link]='Giveyourprospectafreetshirt';
[Link]=[Link];
insertt;//You'llgetanerrorafterthe150th
opp!
}
This code is bulkified and will not hit any governor limits. It uses Lists and will
only do one DML statement no matter how many records are in the trigger:

//DoaninsertDMLonalltasksatonceusinga
List
List<Task>taskList=newList<Task>();
for(Opportunityopp:[Link]){
Taskt=newTask();
[Link]='Giveyourprospectafreetshirt';
[Link]=[Link];
[Link](t);
}
inserttaskList;//Noticethisisoutsidetheloop

You might also like