Design Patterns for Bulkifying an Apex Trigger
Design Patterns for bulkifying an Apex Trigger
As Salesforce is built on the multi-tenant architecture, it has multiple platform limits which govern the uses of resources at one particular transaction. As the Apex trigger can operate on multiple records at the same time, it can easily breach the governor limits and therefore it is suggested that Apex trigger should be bulkified before it is moved to production.
Benefits of bulkified Trigger are many for e.g.:
- Better performance
- Consume fewer resources and time over the server
- It rarely breach the governor limits
To bulkify trigger means to work on collections of sObject instead of individual records. Bulk design concept works on all sObject in the context variable. Normally trigger executes on one record at a time if the origin of the execution was through the user interface, but if the origin was a bulk API or DML then the trigger executes on the whole set rather than one record at time, therefore we should keep in mind that the trigger may perform on the collection of sObject and it is a best practice to bulkify the trigger.
Let have a look in the below example:
Trigger FirstBulktrigger on Account(before insert){
For(account test : Trigger.New){
Test.description = ‘Bulk Trigger was used’;
}
}
In the above Apex trigger, we traverse through each and every account record available in the Trigger.New context variable and set the description of each account to ‘Bulk Trigger was used’.
Performing SOQL in a bulkified Trigger
SOQL is used in Trigger for so many reasons and one reason could be to fetch the related records. To fetch the related records of all the available records in the Trigger’s context variable, we are likely to hit the platform limits which 100SOQL per synchronous transaction.
The best practices suggest the following code pattern:
- Avoid writing SOQL in for loops in a Trigger.
- Always fetch the required amount of records in the Query by using where filter clause.
- We should try to use inner query wherever possible.
Let’s have a look at the below example:
Trigger BulkSOQlExampleTrigger on Account( before insert){
List<account> accountListWithOpp = [select Id, Name (select name from contacts)from Account where Id IN : Trigger.New ];
For(Account acc : accountListWithOpp){
Contact[] relatedContacts = acc.contacts;
}
}
We can perform a bulk DML as well in a similar way:
Trigger BulkSOQlExampleTrigger on Account( before insert){
List<account> accountListWithOpp = [select Id, Name (select name from contacts)from Account where Id IN : Trigger.New ];
For(Account acc : accountListWithOpp){
Contact[] relatedContacts = acc.contacts;
}
For(Contact con : relatedContacts){
Con.lastname = ‘Bulk DML’;
}
}
And that’s how we bulkify a Trigger.
Want to know about interview questions related to Apex Trigger then click on the below
[…] Design Patterns for Bulkifying an Apex Trigger […]
[…] can see that the email is sent to the mentioned email in the code. for(Account acc : Trigger.New) { Design Patterns for Bulkifying an Apex Trigger. Design Patterns for bulkifying an Apex Trigger As Salesforce is built on the multi-tenant […]
Hey Datta,
Thanks for this example.