Introduction to Apex Triggers

Introduction to Apex Triggers
Introduction to Apex Triggers

What is an Apex Trigger?

Apex Trigger enables you to perform custom actions before or after events to sObject in Salesforce, such as Insert, Update, Delete and Undelete.
To execute a trigger before or after Insert, Update, Delete and Undelete operations specify multiple trigger events in a comma – separated list.

When we use Trigger?

Typically, we use trigger to perform operations based on some specific criteria, to modify related records, same record or restrict certain operations from happening.

Types of Triggers

  1. Before Trigger
  2. After Trigger

When to Use Before Trigger?

Before Trigger are used to update or validate record values before they’re saved to the database.

When to Use After Trigger?

After Trigger are used to access field values that are set by the system (such as a record’s Id or LastModifiedDate field), and to effect changes in other records.

Trigger Syntax

trigger TriggerName on <OjectName> (List of trigger_events comma separated){
 code_block
 }
Note: In trigger you do not have to commit data manually, it automatically saves into database.

Before Trigger

  • Before Insert
  • Before Update
  • Before Delete

Before Insert
These triggers fires when we try to insert a new record into an object. All the statement written in
trigger block are executed before new records are saved in database.
For Eg.
When inserting new Account record if account with same name exist, we should prevent the insert
operation.
Syntax:
Trigger AccountInsert on Account (before Insert){ //Code block
}

Before Update
When we modify the value of a record and click on update, the before trigger is called on object and
its code block is executed.
To perform any changes in the values of new record, we can do it directly using trigger.new in before
trigger.
For Eg.
We have a condition that will prevent a user from creating the lead that already exist as a contact.
We will use lead/contact email address to prevent duplicates.
Syntax
Trigger findDuplicates on Lead (before update){
//code block
}

Before Delete
Trigger.old can be used in before or after delete to store the list of records which we are trying to
delete.
For Eg.
When we are trying to delete customer record delete all corresponding child record from test object.
Syntax
Trigger customerdel on Customer__c(After Delete){
//Code block
}

After Trigger

  • After Insert
  • After Update
  • After Delete
  • After Undelete
  • After Update

After Insert
This tigger are fired when new records are successfully saved in the database. This Trigger.new is
used to refer to the list of new records which were inserted.
For Eg.
When a new contact is created for any account, update the account phone with contact phone.

Syntax:
Trigger UpdatePhone on Contact(After insert){
// code block)
}

After Update
The operation written in after triggers fires when the changes done on records saved to database.
In after update trigger operation, we can only read the data from trigger. To perform any changes on the records in after update trigger we have to write DML statements. Trigger.old and Trigger.new can be used in update event.

After Delete
Trigger.Old can be used in after delete to store the list of records which we are trying to delete.
After Delete can only perform read only operation.
For Eg.
When we are trying to delete Account record delete.
Syntax:
Trigger accountdata on Account(After Delete){
//code block
}

After Undelete
When we undelete the record from recycle bin, then the operation written in after undelete trigger is executed.
Trigger.new we are using because it holds the record which we have undeleted.

Parul Singh

I’m a Salesforce Certified Service Cloud Consultant, Certified Administrator & Certified Platform Developer. I have 2 years of experience, currently working on Field Service Lightning, Lightning Flows and Learning Lightning Web Component. I was featured in the top Salesforce Bloggers of 2018 by www.forcetalks.com

You may also like...