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
- Operating on all records of the trigger.
- Performing SOQL & DML on collections of sObjects instead of single sObject at a time.
- 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.
- 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';
}
}
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;
}
}