Master Everything About Triggers In Salesforce – Everything You Need To Know.

What exactly is an Apex Trigger?

Apex Triggers, specific to Salesforce, are utilized for various tasks such as lead conversions. They are a type of code segment that executes before a record is inserted, updated, or deleted from the database. For detailed guidance on creating an Apex trigger in Salesforce, refer to the article “How to Create Sample Apex Trigger in Salesforce” available at https://mindmajix.com/salesforce/how-to-create-sample-apex-trigger-in-salesforce.

In Salesforce, triggers are categorized into two main types:

  • Before Trigger: This type of trigger is employed to validate or modify record contents before they are saved to the database. It allows for examining the record prior to database insertion, enabling the validation of data using specified parameters or codes.
  • After Trigger: Conversely, an after trigger in Salesforce is used to retrieve system-defined field values and make alterations based on the transaction. Essentially, the after trigger adjusts values based on the data input in another record. For comprehensive knowledge on triggers, consider enrolling in Mindmajix’s Advanced Salesforce Training.

Large Triggers

By default, all Salesforce triggers are considered bulk triggers, capable of processing multiple records simultaneously. These bulky triggers are suitable for handling large transactions and single-record changes, including:

  • Data imports
  • Bulk API calls
  • Mass actions
  • Apex procedures and triggers that execute significant DML statements recursively.

Trigger Syntax

The syntax of a trigger is fairly simple. Here’s an example:

Trigger TriggerName on ObjectName(trigger_events)

{
    //block_of_code
}

Now, let’s explore the different keywords used in the trigger syntax:

Trigger Name: This is the name you choose to identify your trigger. Object Name: It specifies the entity on which the operation will be performed. Trigger_events: This refers to a comma-separated list of one or more events. For instance:

  • Before Insert: Executes the block of code before a new record is inserted.
  • Before Update: Performs actions before the object is updated with a new record.
  • Before Delete: Deletes the record before executing the code block.
  • After Insert: Runs the code block after the record has been inserted.
  • After Update: Executes after the code block has been processed.
  • After Delete: Allows record deletion after executing the code.
  • After Undelete: Used to restore an item from the Recycle Bin.

For example, consider the following code snippet illustrating the creation of an object and a trigger in Salesforce:

trigger ABC on contact(before insert)

{ 
    contact x = new contact();
    if (x.email==null)
}

Comparing Triggers and Workflows in Salesforce

When beginning to work with triggers, it’s common to get confused between workflows and triggers in Salesforce. Let us clarify this for you. If Salesforce were to create two separate products, there would certainly be a notable difference between them. What sets them apart now?

Salesforce Workflow:

  • It is a fully automated procedure capable of initiating an action based on assessment and rule criteria.
  • It is not feasible to do DML operations within a process.
  • A workflow can be obtained for an item.
  • A query cannot be created from the database.

Trigger in Salesforce:

  • It is a block of code that is run before or following the updating or inserting of a record.
  • A single trigger can include more than 15 DML actions.
  • Over 20 SOQLs from the database can be utilized in a trigger.
  • You can retrieve triggers that span several objects and are associated with that item.

Workflow Constraints That Triggers in Salesforce Transcend

  • Workflows are not capable of creating or updating a distinct object.
  • When utilizing workflows, you cannot refer to specific fields.
  • Your workflow will be limited to field updates and email.