What are Apex Triggers? | All You Need to Know

Before reading this blog, you must have basic knowledge of Apex Language and SOQL. 

Apex triggers can invoke apex code and enable you to perform custom actions before or after changes to Salesforce records such as insertions, updates or deletions. Trigger enhance the functionality of Apex language as they can perform repetitive tasks which might be time taking.

Trigger can accomplish tasks that cannot be done via point and click tool. They can redirect the execution of any program based on DML operations. It is advisable to not write the lengthy apex code in trigger, as it might become complex to detect the bugs. The good practice is to keep the trigger and handler class separately, where handler class holds the execution part, and the trigger takes care of calling the class.

A trigger is apex script that executes before or after the following types of operations:

  • Insert
  • Update
  • Delete
  • Merge
  • Upsert
  • Undelete 

dont miss out iconDon't forget to check out: Crash Course on Apex Triggers Salesforce | Complete Guide with Real Time Scenarios

For example, you can have a trigger run, after records have been deleted from the database, before an object's records are inserted or even after a record is restored from the Recycle Bin.

Triggers can be defined for top-level standard objects, such as Contacts or Account, custom objects, and some standard child objects. Triggers are active by default when created, and fired automatically when database event occurs. 

Here’s the trigger syntax:

trigger TriggerName on ObjectName (trigger_events)  
{ 
     code_block 
}

Types of Triggers

There are two types of apex triggers, Before trigger and After trigger. 

  • Before trigger: It executes just before any insert operation takes place. They can be used to set field values before saving any record. 
  • After trigger: It executes just after any insert operation finishes. They can only be read, no changes are allowed.

dont miss out iconCheck out an amazing Salesforce infographic here: ABC's of Salesforce's Apex Coding Language

Points to consider while writing Triggers-

  • Write Bulk trigger: When an action fires a trigger for processing on single record, the trigger only consider one record, but sometimes API request for multiple records, that’s where bulk trigger comes in picture. Apex triggers are able to operate on a record set rather than a single record. It helps consuming less server power, better performance and quicker DML  

The below example shows the difference:

This example only considers one record 

Account acc = Trigger.new[0];

This example considers the all records of Account sObject.

For ( Account acc : Trigger.new )
  • Write Bulk SOQL: SOQL is a powerful tool to operate on large chunks of data. The SOQL must be bulkified in order to escape governor limits. Salesforce works on multitenant architecture where all resources are shared, so governor limits are always considered while performing DML operations.

It is advised to not write SOQL queries inside loops.

For ( Account acc : Trigger.new) 
{ 
     List<Account> newList = [ Select Name, Id from Account ]; 
}

This is the right approach to write bulk SOQL-

List<Account> newList  = [ Select Name, Id from Account ]; 
For ( Account acc : newList) { 
}

Responses

Popular Salesforce Blogs