Why And How To Bulkify Your Code?

Introduction

Salesforce is a multitenant environment. Its single instance is used by every organization. So, to prevent the org from consuming more resources, Governor Limits came into the picture. Salesforce has introduced Governor Limits for each and everything in SF org.

If we don’t adhere to the Governor Limits which are more likely rules and regulations set by SF to prevent more usage than it should be, an Exception will be thrown.

For e.g. the number of SOQL queries per transaction should not be more than 100 synchronously. Bulkification of code will avert the exceptions caused by this.

What is meant bulkifying the code?

When our apex code is behaving similarly for single records as well as for multiple records or it is capable of handling more than one record at a time, we can say that our code is bulkified.
Bulkified code ensures that our Apex code runs smoothly without any exceptions.

Why do you need to bulkify your code?

It is important to make our code bulkify as it will prevent developers from facing multiple exceptions due to exceeding Governor Limits. It becomes important when more numbers of records are getting imported as bulkifying our apex code can handle these records efficiently. Suppose, we are importing 300 records using Data Loader, if our code is not bulkified, we might get an exception ‘Too many SOQL Queries: 101’.

Bulkifying apex code means performing DML and SOQL on collections of sObjects instead of single sObjects at a time.

How to bulkify your code?

If we bulkify our trigger, it will operate on all sObjects in the context of the trigger. By default, it works on a single record if we do any changes from UI but if we are inserting records using Data Loader, we get a set of records. So, It would be better to create a trigger assuming It will work on a collection of records.

Perform Bulk SOQL

Try avoiding writing SOQL queries in for loop as It might not follow the Governor Limits and can give us a SOQL 101 exception.
Use fewer queries to avoid exceeding query limits synchronously (100 queries) and asynchronously (200 queries)

Perform Bulk DML

Writing DML statements in for loop can also hit DML statements limits which are 150 per transaction. This can be done with the help of collections that store our data or records temporarily.
We can perform DML operation on collections of objects in Salesforce as our resources would be inefficiently used if we use DML on each object individually.

Bulkify your code by combining SOQL queries

trigger ContactAcc on Contact (after insert) {
Set accConIds = new Set();
for(Contact cc : Trigger.new){
if(cc.AccountId != null){
accConIds.add(cc.AccountId);
}
}
List accList = new List();
for(AggregateResult con : [SELECT AccountId, COUNT(Id)
numOfCon FROM Contact WHERE
AccountId IN :accConIds AND
Active__c = true GROUP BY
AccountId]){
Account acc = new Account();
acc.Id = String.valueOf(con.get('AccountId'));
acc.Active_Contacts__c =
Integer.valueOf(con.get('numOfCon'));
accList.add(acc);
}
update accList;
}

 

Bulkification Example

trigger UserAcc on Account (before insert, before update) {
Set accIds = new Set();
for(Account acc : Trigger.new){
accIds.add(acc.OwnerId);
}
Map userMap = new Map([SELECT Id, Name FROM User WHERE Id IN :accIds]);
if(userMap.size() > 0 && userMap.size() != null){
for(Account accs : Trigger.new){
if(Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(accs.Id).OwnerId != accs.OwnerId)){
accs.Sales_Rep__c = userMap.get(accs.OwnerId).Name;
}
}
}
}

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.