Free Salesforce Developers Tutorials >

Chapter - 9 Apex Triggers >

Best Practice in Triggers

Best Practice in Triggers

What You’ll Learn


tickWhat are Bulkified Triggers?

tickMain characteristics of Bulkified Trigger

ticBulkified Triggers – Coding Practices

S2 Labs

Best Practices In Apex Triggers

There are various best practice concepts that we need to keep in mind while writing triggers. One of them is Bulkified Triggers which are explained below:

Bulkified Triggers

Bulkifying triggers mean writing apex triggers using a bulk design pattern so that triggers have better performance and consume fewer server resources.

As a result of it, bulkified code can process a large number of records efficiently and run within governor limits on the force.com platform.

Main Characteristics Of Bulkified Trigger

  1. Operating on all records of the trigger.
  2. Performing SOQL & DML on collections of sObjects instead of single sObject at a time.
  3. Using maps to hold query results organized by record id. Avoid query within a query and save records in the map which later can be accessed through a map rather than using SOQL.
  4. Using Sets to isolate distinct records.
 trigger ApexTrigger on Lead(before insert) {
            /**** Bulkified Triggers ****/
            Lead l = Trigger.new[0]; // will only update the value for first record 
            l.Rating = 'Warm'; //Avoid using Triggers like this 
            //Always use Triggers in Bulkified form 
            for (Lead l: Trigger.new) // will iterate through all the new records 
            {
                l.Rating = 'Warm';
            }
        }

Salesforce Developer

Bulkified Triggers – Coding Practice Examples

Example 1:

Avoid creating Triggers that work only for individual records but not for entire datasets:

trigger testTrigger on Acount__c(before insert) {
            Acount__c acc = Trigger.New[0];
            acc.Address__c = 'Temporary Address';
        }

Create Triggers that use loops to help iterate over a list of records within a transaction:

 trigger testTrigger on Acount__c(before insert) {
            integer i = 1;
            for (Acount__c acc: Trigger.new) {
                acc.Address__c = 'Test Address ' + i;
                i++;
            }
        }

Example 2:

In this code, let’s assume 200 Account records are updated, so the “for” loop would iterate over 200 records.

trigger BranchTrigger on Branch__c(before update) {
            for (Branch__c br: Trigger.new) {
                List < Acount__c > accList = [SELECT Name, Account_Name__c, Address__c, Balance__c FROM Acount__c
                    WHERE Acount_of_Branch__c = : br.id
                ];
                System.debug(accList);
                // Perform specified logic with queried records 
            }
        }

Now let’s look at a good example of querying bulk data and iterating it.

trigger BranchTrigger on Branch__c(before update) {
            List < Acount__c > accList = [SELECT Name, Account_Name__c, Address__c, Balance__c FROM Acount__c
                WHERE Acount_of_Branch__c IN: Trigger.New
            ];
            System.debug(accList);
            for (Acount__c acc: accList) {
                // Perform specified logic with queried records 
            }
        }

Example 3:

DML statements are also bound by Governor Limits; you can call only 150 DML operations in a transaction.

trigger BranchTrigger on Branch__c(before update) {
            List < Acount__c > accList = [SELECT Name, Account_Name__c, Address__c, Balance__c FROM Acount__c
                WHERE Acount_of_Branch__c IN: Trigger.New
            ];
            System.debug(accList);
            integer i = 0;
            for (Acount__c acc: accList) {
                acc.Address__c = 'Test Address ' + i;
                i++;
                update acc;
            }
        }

Let’s look a correct coding example where we have instantiated another Account object list called “accToUpdate.”

trigger BranchTrigger on Branch__c(before update) {
            List < Acount__c > accToUpdate = new List < Acount__c > ();
            List < Acount__c > accList = [SELECT Name, Account_Name__c, Address__c, Balance__c FROM Acount__c
                WHERE Acount_of_Branch__c IN: Trigger.New
            ];
            System.debug(accList);
            integer i = 0;
            for (Acount__c acc: accList) {
                acc.Address__c = 'Test Address ' + i;
                i++;
                accToUpdate.add(acc);
            }
            if (!accToUpdate.isEmpty()) {
                update accToUpdate;
            }
        }

Salesforce LWC Training

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *