This post is all about “Bulkify Your Code” where I will explain –
- What is the meaning of this phrase?
- Why this is so important?
- Explain with Use Cases
So let’s start with What is the meaning of “Bulkify Your Code” –
I have the below two custom objects in my org with field details as per the below screenshot
My requirement is that if an Airport record is having the Operational checkbox true, then create a country record where Country.Country Name = Airport.Country. Very simple right!!!
But this can lead to many problems.
So let’s write the trigger for that first –
trigger AirportTrigger on Airport__c (after insert) {
for(Airport__c anAirport : Trigger.new){
if(anAirport.Operational__c){
Country__c aCountry = new Country__c(
Name=anAirport.Country__c
);
insert aCountry;
}
}
}
Very simple trigger. What the trigger is doing is simple iterating over Trigger.new (it is returning a list of Airport records that are getting inserted) and checking whether the Operational__c field is true or not. If true, then creating a new Country record with Country name same as airport’s country name.
This trigger will work fine when you create a single airport record from Salesforce, because, in that case, Trigger.new will return you only 1 record and which will perform max 1 DML operation (when Operational__c is true).
But the problem will occur when we will insert multiple airport records with import wizard.
Now what I will do is that I will prepare a csv file with 1000 airport records in the below fashion.
Now if I try to import these 1000 airport records with import wizard, below is what I am getting –
Now when I checked the debug log, below is the error message I am getting –
Ok – got it!!. So we are getting the error “Too many DML statements“. This is the Governor Limit set by Salesforce, which states as a developer you can execute at max 150 DML statements. But what we are doing is –
When we tried to import 1000 new airport records, Trigger.new did batch up mass insert upto 200 records at a time. Each of these 200 records executed one new insert statement for Country object. So total number of DML operation becomes 200, which is beyond the Salesforce’s Governor Limit (150 DML Operations).
Now let’s play more with Salesforce Governor Limit and change the import file like below –
As you can see here, I have changed Operational field alternatively. Now when I tried to import this file(File contains 1000 Airport records), below is what I am getting –
Success!! Do you know the reason for the success. Ok. Let me tell you.
Trigger.new again did the batch up of mass insert upto 200 records at a time, but out of these 200 records, only 100 records were having Operational__c field set as true. So total number of DML operation were 100 which was within Salesforce’s Governor Limit.
But still our code is not efficient as with this code, we can’t insert 1000 records (our first csv files). So we have to make our code efficient and the way we can do that is –
trigger AirportTrigger on Airport__c (after insert) {
List<Country__c> allCountries = new List<Country__c>();
for(Airport__c anAirport : Trigger.new){
if(anAirport.Operational__c){
Country__c aCountry = new Country__c(
Name=anAirport.Country__c
);
allCountries.add(aCountry);
}
}
if(allCountries.size() > 0){
Database.insert(allCountries);
}
}
Now with the above code, when I tried to insert 1000 airport record with Operational__c field set to true for all, below is what I am getting –
Great!! So the code is now efficient and can handle any number of insert. The changes we did are as follows –
- Created a list which will hold all the country records which need to be inserted into Salesforce org.
- Finally if the size of the list is greater than 0, then execute Database.insert which will insert all the country records.
Please check my next post with another example –
“Bulkify Your Code” – Another Example with Use Case