Apex Trigger in Salesforce

All About Apex Trigger in Salesforce | Learn How to Create a Trigger

Trigger is a piece of code which executes or fired whenever a record is created, edited or deleted. It’s a concept of automation processes and solving complex scenarios. These are stored procedures which are executed whenever any particular event occurs. We can perform custom actions before or after record changes to Salesforce records. 

For example, they are used to perform operations such as modifying related records or restricting certain operations. They are also used to execute SOQL and DML or call the custom apex method. 

Advertisement

These are used to perform tasks which can't be done by point-and-click tools in the Salesforce user interface. Triggers are active by default which means Salesforce automatically fires active triggers when the specified database events occur. 

Trigger Syntax

trigger TriggerName on ObjectName(trigger events){ 
    //Code block 
    // 
}

dont miss out iconDon't forget to check out: All You Need to Know about Batch Apex in Salesforce

Apex triggers are of two types: Before and after which will fire before or after inserting the data into the database respectively. 

  • Before Trigger - It is used to update or validate record values before we save them into the database. 
  • After Trigger - It is used to access field values which are set by the system and to affect the changes in other records. Those records are read-only. 

A Trigger executes the following operations: 

  • Insert
  • Update
  • Delete
  • Merge
  • Up
  • Undelete

Steps to Create a Trigger

  1. In the developer console, go to File then New then click Apex Trigger.
  2. Enter any name for your trigger example myFirstTrigger, then select Contact for the sObject. Click Submit.
  3. Write your code. For example:
    Trigger myFirstTrigger on Contact(before insert){ 
        system.debug(‘Hello World!’); 
    }
  4. To save, press Ctrl+S.
  5. To test a trigger we have to create an account.
  6. Click Debug then Open Execute Anonymous Window.
  7. In the new window, add the following and then click Execute.
    Contact CT = new Contact (lastName = ‘Test Trigger’); 
    Insert CT;

Trigger Context Variables

Using Trigger Context Variables 

  1. IsExecuting – It returns true is the current context of the apex code is trigger.
  2. IsInsert – It returns true if trigger is fired due to the insert operation.
  3. IsUpdate – It returns true if trigger is fired due to the update operation.
  4. IsDelete – It returns true if trigger is fired due to delete operation.
  5. IsBefore – It returns true if trigger is fired before saving any record.
  6. IsAfter - It returns true if trigger is fired after saving any record.
  7. IsUndelete – It returns true if trigger is fired after a record is recovered from the Recycle Bin.
  8. New – It returns a list of new versions of sObject records.
  9. Old – It returns a list of old versions of sObject records.

dont miss out iconCheck out another amazing blog by Alok here: Validation Rules in Salesforce | A Brief Guide by Salesforce Developer

Bulk Trigger

All triggers are Bulk triggers by default and can process multiple records at a time, Bulk triggers can handle both single record updates and bulk operations including:

  •  Data Import 
  •  Lightning Platform Bulk API calls 
  • Mass actions for record changes 
  • Recursive Apex methods and triggers which invoke Bulk DML statements 

Trigger Exceptions

Trigger can be used to prevent DML operations from occurring by calling the addError() method on a record or a field. When used on Trigger.new records in insert and update triggers, and on Trigger.old records in delete triggers, the custom error message is displayed in the application interface and logged. 

Responses

Popular Salesforce Blogs