Triggers in Salesforce - Get Started
What is a Trigger?
A Trigger is an Apex script, which we can use in many situations. The use of trigger is, it will help you to call a specific piece of code when something happens in Salesforce org. Eg: Record inserted / Record edited / Record deleted etc. In such cases, we can use Trigger.
What is Trigger Syntax?
Trigger TriggerName on ObjectName (Trigger Events) { Code block }
Types of Triggers
There are two types of triggers:
- Before Triggers - In this type of trigger, code is executed before the record gets saved in the database.
- After Triggers - In this type of trigger, code is executed after the record gets saved in the database.
Don't forget to check out: Deleting Apex Classes / Apex Triggers From Production Using Workbench | Salesforce Tutorial
Trigger Context Variables
- isInsert - Returns true if the trigger got fired because of the insert operation.
- isUpdate - Returns true if the trigger got fired because of the update operation.
- isDelete - Returns true if the trigger got fired because of the delete operation.
- isBefore - Returns true if the trigger got fired before any record was saved in the database.
- isAfter - Returns true if the trigger got fired after all records were saved in the database.
- isUndelete - If a record gets recovered after deletion it returns true.
- isExecuting - if the current context for the Apex code is only a trigger, it returns true.
- new - Returns a list of records with new versions of them.
- old - It gives a list of old versions of the records in return.
- newMap - Map of new versions of the records.
- oldMap - It is a Map of old versions of the records.
- size - The total number of records in both trigger old and trigger new.
Trigger Events
- beforeinsert
- afterinsert
- beforeupdate
- afterupdate
- beforedelete
- afterdelete
- afterundelete
Example Code
Requirement: Show error on duplicate contact creation.
Handler class code:
public class DuplicateContactErrorHandler { public static void DuplicateContactErrorMethod(List<Contact> contactList){ //created a list of contacts in method Set<String> new_EmailSet = new Set<String>(); //set for new email strings Set<String> existingEmailSet = new Set<String>(); //set for existing email string for ( Contact con : contactList ) { //for loop on contact if ( con.Email != null ) { //checking if email field is null or not newEmailSet.add(con.Email); //if not null then email is added in the set } } //query to call existing contacts with email List<Contact> existing_ContactsList = [Select Id, Email From Contact Where Email IN: new_EmailSet AND Email != null]; for (Contact con : existing_ContactsList ) { //for loop on existing contact list existingEmailSet.add(con.Email); //email ids added in the existing email set } for ( Contact con : contactList ) { //for loop on new contact list if ( existingEmailSet.contains( con.Email ) ) { //if email matches email in the existing set con.Email.AddError(' Duplicate Email is not Allowed '); //contact will not be saved, it will show error } else { existingEmailSet.add(con.Email); //else it's email will be saved in existing email set } } } }
Check out another amazing blog by Romil here: Types of Relationships in Salesforce - All You Need to Know
Trigger Code:
trigger Duplicate_Contact_Error_Trigger on Contact (before insert, before update) { if(Trigger.isinsert || Trigger.isupdate){ DuplicateContactErrorHandler.DuplicateContactErrorMethod(trigger.new); } }
Responses