Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home1/oijoiv2f/public_html/wp-content/themes/entaro/template-posts/single/inner.php on line 23

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.:

  1. Better performance
  2. Consume fewer resources and time over the server
  3. 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:

  1. Avoid writing SOQL in for loops in a Trigger.
  2. Always fetch the required amount of records in the Query by using where filter clause.
  3. 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

Interview Questions Apex Trigger

Sumit Datta

Sumit Datta

I am a 5x Certified Salesforce developer with overall 7 years of IT experience and 5 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

3 Comments

  1. […] Design Patterns for Bulkifying an Apex Trigger […]

  2. […] 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 […]

  3. August 8, 2017 Reply

    Hey Datta,

    Thanks for this example.

Leave a Comment

Your email address will not be published.