Understanding Bulkify Trigger and Salesforce Trigger Best Practices

Today, we’re going to talk a little bit on the logic for bulkify trigger Salesforce. This is a somewhat abstract programming concept, and not something that we can teach in a simple set of steps, so with that in mind, we’re just going to talk a little bit about the notion behind bulkifying, its benefits, and the programming concepts involved in this mentality. This article is very much intended for someone with some level of programming understanding, if only a basic level, so if you’re not familiar with any of the concepts presented in this discussion, it’s suggested that you stop reading this for a moment, and take in a couple short introductory paragraphs to programming logic. It will greatly clarify what we’re talking about here. The audience listens to the acting talking about salesforce trigger tutorial Want to learn about writing Salesforce Apex Trigger? Click here.

Salesforce Trigger Best Practices and Bulkify Trigger

Bulkifying is the process of designing code modules so that they can handle large quantities of data in a single call or calculation, rather than individual data sets over repeated calls from a higher code body. This concept is an evolution of the notion of making general libraries that handle large tasks discretely rather than individual tight loops to handle instructions. In bulkifying, you’re improving efficiency of the code being run, as each time something has to be called to handle a task, such as a trigger or a class, it has to be loaded in and out of memory, and each returned result has to be added to a collection after the fact, in your higher level namespace. So, how is bulkfying done? Well, basically it involves using collections or arrays (depending on language), and FOR logic to serially process multiple calculations that otherwise would be done discretely, requiring multiple sequential calls of the code. Let’s take a look at a simple example of some Apex code both not bulkified, and bulkified.

Not Bulkified:

trigger accountTestTrigger on Account (before insert, before update) { //Notice that this handles only one account, regardless of quantity Account MyAcct = Trigger.new[0]; List<Contact> MyContacts = [select id, salutation, firstname, lastname, email from Contact where accountId = :MyAcct.Id]; }


trigger accountTestTrigger on Account (before insert, before update) { List<String> accountNames = new List<String>{}; //Loop through all records in our collection for(Account a: Trigger.new){ //Concatenate the Name and billingState onto the Description field a.Description = a.Name + ‘:’ + a.BillingState } }


As you can see, Salesforce Trigger Best Practices and bulkying your code is all efficient use of collections and an extra FOR loop to go through the plural data and handle them all within a single operation, to prevent the need for the entire class or trigger to be called again. This reduces the number of triggers that must be created greatly. For additional information go to salesforce trigger tutorial page, and gain better results. This simple bit of programming wisdom is all you need to understand bulkify trigger Salesforce.
Amanda is the Lead Author & Editor of Rainforce Blog. Amanda established the Rainforce blog to create a source for news and discussion about some of the issues, challenges, news, and ideas relating to Salesforce usage.