When To Use Before Vs After Triggers In Salesforce?

by | Jul 31, 2022 | Salesforce

Introduction

A trigger is an apex code executed before or after DML events occur. A trigger is a class behind the scenes and it has variables like New, NewMap, Old, and OldMap. Triggers should be bulkified. It should work for single records as well as multiple records in the same way.

Concept of Triggers in Salesforce

A trigger is an Apex Component that gets executed automatically once DML is fired on any Object. Suppose we have a trigger written on the Account object before inserting and before updating events. Then if the user is trying to insert or update the record of the Account, then the trigger will fire based on the condition provided.

It means triggers are Object-specific (trigger can be created on a single object at a time) and Event-centric.

Types of Triggers in Salesforce

We primarily have two types of triggers in Salesforce

1. Before Trigger

This is mainly used if we want to update or validate the values of a record before the record is saved into the database. It means the first record will be validated, and then it is saved before the trigger. We are not required to write any DML events before the trigger.

2. After Trigger

This type of trigger is usually used when data needs to be updated into a separate object due to a change.

They execute after final changes have been made to the database.

After Trigger is fired once DML is committed in any object, the records that fire the After Trigger are read-only.

When to use Before Vs After Triggers in Salesforce?

When we need to validate or update the data or record values before the record is saved, then use Before Trigger. When we want to access the field values that are set by the system like the LastModifiedDate field and to affect the changes in other records, use After Trigger.

The records which Initiated the database operation are read-only in After Trigger. If that record initiated the trigger’s execution, we could not modify any field values in After Trigger. For example, We have a trigger on the Account object with the After update DML event, and we want to update the field value on the Account object. In this case, Users won’t be able to change the field value as it would be in read-only mode. So, use Before Trigger in this scenario instead of After Trigger.

Examples

Example 1

trigger ContactDeletion on Contact (before delete) {
//Trigger.old contains list of contacts which are getting deleted
for(Contact cc : Trigger.old){
if(cc.AccountId != null){
cc.addError('You cannot delete contact having associated account');
}
}
}

 

Example 2

trigger AccOppTrigger on Account (after insert) {


List oppRec = new List();
for(Account acc : Trigger.new){
if(acc.Industry == 'Banking'){
Opportunity opp = new Opportunity(Name = acc.Name, StageName = 'Prospecting', AccountId = acc.Id, CloseDate = Date.today().addDays(15));
oppRec.add(opp);
}
}
if(!oppRec.isEmpty()){
insert oppRec;
}
}

 

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.