Triggers in Salesforce - Learn All You Need to Know

What is Apex Trigger?

Apex Triggers are used to refer to triggers in Salesforce. These are different and are accessible for particular tasks such as lead conversions. A trigger is a section of code that runs before the actual record is added, modified, or removed from the database. You can create an Apex trigger in Salesforce by reading this article: How to Create Sample Apex Trigger in SalesForce https://mindmajix.com/salesforce/how-to-create-sample-apex-trigger-in-salesforce

Triggers in Salesforce are classified into two categories:

Before Trigger: In Salesforce, this sort of trigger is used to either change or verify the contents of a record prior to saving them to the database. Thus, the before trigger examines the record before saving it. Specific parameters or codes can be defined to validate data prior to it being prepared for database entry.

After Trigger: In Salesforce, this sort of trigger is often used to retrieve the system-defined field values and affect any changes to the transaction. In simple words, the after trigger modifies the value based on the data put in another record. You can learn more about Triggers in Mindmajix’s Advanced Salesforce Training

Bulky Triggers

By default, all triggers in Salesforce are bulky triggers, which means they may handle many records simultaneously. Bulky triggers can be used to do large transactions and single-record changes, such as the following:

  • Data Import
  • API calls in bulk
  • Mass Actions
  • Apex procedures and triggers that recursively execute significant DML statements

dont miss out iconDon't forget to check out: Learn All About Apex in Salesforce | The Developer Guide

Syntax of Trigger

A trigger's syntax is quite straightforward. Let us take a look:

Trigger trigger Name on Object Name(trigger_events)

{
    //block_of_code
}

Now, let us have a look at the various keywords utilized in the syntax:

  1. Trigger Name: The name with which you wish to identify your trigger. 
  2. Object Name: It specifies the entity on which the operation should be done.
  3. Trigger_events: It  refers to a comma-separated sequence of one or more events, for example:
    • Before Insert: When this event is used, the block of code is run prior to the insertion of a new record.
    • Before Update: When this event is used, the function is performed prior to the object being changed with a new record.
    • Before Delete: When this trigger is used, the record is destroyed prior to the code block being executed.
    • After Insert: In this case, the code block is run first, followed by the record insertion.
    • After Update: This event occurs after the code block has been executed.
    • After Delete: When this trigger is used, you can delete a record following the execution of the block of code.
    • After Undelete: This action is used to restore an item that was sent to the Recycle Bin.

For Example - The code demonstrates how to create an object and a trigger in Salesforce: 

trigger ABC on contact(before insert)

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

Triggers vs. Workflows in Salesforce

When you are first getting started with triggers, it's easy to become puzzled between workflows and triggers in Salesforce. Allow us to resolve this for you. To be sure, if Salesforce built two distinct products, there would undoubtedly be a significant difference between them. What is the distinction now?

Workflow in Salesforce:

  • 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.

dont miss out iconCheck out an amazing Salesforce video tutorial here: Apex Triggers in Salesforce | Concepts and Examples

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.

Author Bio

Vinod Kumar Tanem is a Digital Marketer, and a passionate writer, who is working with MindMajix, a top global online training provider. He also holds in-depth knowledge of IT and demanding technologies such as Cloud Computing, Salesforce, Cybersecurity, Software Testing, QA, Data analytics, Project Management and ERP tools, etc. Follow him on LinkedIn.

Responses

Popular Salesforce Blogs