Apex Trigger is one of the most critical as well as complicated components of salesforce. Not everyone knows how to utilize it in the best manner and that’s why we have prepared a guide that will help you with the same.

Salesforce is one of the top Customer Relationship Management platforms available today. Most enterprises prefer this software to efficiently manage their business operations. It assists your company in performing complex tasks, such as Data Analytics, Marketing Automation, Customer Service, and Platform Development applications.

Apex Trigger at a Glance

Apex Trigger is an indispensable component of Salesforce that facilitates decoding complex scenarios. It is a piece of code that executes or fires whenever a record is created, edited, or deleted. These codes are executed as soon as any defined event occurs in the record. It enables you to perform custom actions before or after events to records in Salesforce.

Apex Trigger is particularly used to execute tasks that can't be accomplished by point-and-click tools in the Salesforce user interface. These triggers are active by default and execute once the specified events occur.

For instance, when you want to perform complex operations on Salesforce such as modifying related records or restraining certain operations, you can make use of Apex Trigger. SOQL and DML are also executed in Salesforce using these automated codes.

 

Types of Apex Triggers

Apex Triggers are mainly of two types: Before and After that fire codes before or after adding the defined data into the database respectively.

  • Before Trigger - Specifically used to update or validate record values before saving them into the database. 
  • After Trigger -  Particularly used to access field values set by the system and to influence changes in other read-only records. 

Apex Trigger in Salesforce enforces the following operations:

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

Steps to Create a Trigger

  • In the developer console, visit File >New > and click on Apex Trigger.
  • Type a new name for the file, for instance: TryTrigger, then select Contact for the sObject and click Submit.
  • Write the code you want to apply the trigger on.
  • Finally, save it by clicking Ctrl+S.
  • To test a trigger you need to create an account first.
  • Then, click Debug >Open Execute Anonymous Window.
  • In the new window, add the following syntax and click on Execute.
  1. Contact CT = new Contact (lastName = ‘Test Trigger’); 
  2. Insert CT;

Trigger Context Variable

  • IsExecuting – Returns true if the current context of the apex code is trigger.
  • IsInsert – Returns true if the trigger is fired because of the insert operation.
  • IsDelete – Returns true if the trigger is fired because of the delete operation.
  • IsUndelete – It returns true if the trigger is fired after a record is recovered from the Recycle Bin.
  • IsUpdate – Returns true if the trigger is fired because of the update operation.
  • IsBefore – Returns true if the trigger is fired before saving any record.
  • IsAfter - Returns true if the trigger is fired after saving any record.
  • New – Returns a list of new versions of sObject records.
  • Old – Returns a list of old versions of sObject records.

We hope this comprehensive guide helped you clear everything concerning Apex Trigger in Salesforce. The primary role of the same is to automate your data management and workflow in Salesforce. Further, it enables you to bulk up your code, restrict hard-coded IDs, test multiple scenarios, and revamp your codes. Therefore, we can conclude that Apex Trigger is an indispensable component of Salesforce.

Facing issues with Apex Trigger Implementation?

Don’t worry as we are here to help you out. AtoCloud is the leading Salesforce Development Company in India which offers optimum services. We have a team of experts who are well-versed in Salesforce & can help you with Trigger and more.