What Is Trigger in Salesforce?

Triggers in Salesforce are called Apex Triggers. These are distinct and are available specifically for common and expected actions like lead conversions.

Here are the topics you will learn in this section:

A trigger in Salesforce is an Apex code which is used to perform an operation before or after a record is operated. These operations can be:
What Is Trigger in Salesforce
There are primarily two types of Apex Triggers:

  • Before Trigger: This type of trigger in Salesforce is used to either update or validate the values of a record before they can be saved into the database. So, basically, the before trigger validates the record first and then saves it. Some criteria or code can be put to check before the data is ready to be inserted into the database.
  • After Trigger: This type of trigger in Salesforce is used to access the field values set by the system and affect any other changes in the record. So, the after trigger makes changes in the value from the data inserted in some other record.

Watch the following video for a better understanding of Salesforce

Triggers in Salesforce What Is Trigger in Salesforce? Triggers in Salesforce are called Apex Triggers. These are distinct and are available specifically for common and expected actions like lead conversions. Here are the topics you will learn in this section: What Is Trigger in Salesforce? Bulky Trigger Trigger Syntax Trigger Context Variables Triggers

Bulky Trigger

All triggers in Salesforce are, by default, bulky triggers. This means that you can process multiple records at a time. Bulky triggers can handle bulk operations and single-record updates like:

  • Importing data
  • Mass actions
  • Bulk API calls
  • Recursive Apex methods and triggers invoke bulk DML statements

Trigger Syntax

The syntax of a trigger is very simple. Let’s first look at its basic syntax:

trigger triggerName on Objectname(trigger_events)
{
//code_block
}

Let’s discuss various keywords used in the syntax:

    • triggerName is the name you would want to give your trigger.
    • Objectname is the object on which the action needs to be performed.
    • Trigger_events are the comma-separated list of one or more events such as:
      • Before insert: When using this event, the code block gets executed before a new record is inserted.
      • Before update: When you use this event, the code will get executed before a new record is updated in the object.
      • Before delete: When you’re using this event, the record will get deleted before the execution of the code block.
      • After insert: The code block gets executed first, and then the insertion of the record is done.
      • After update: In this event, the updation of a record is done after the execution of the code block.
      • After delete: When you’re using this event, you will be able to delete a record after the execution of the code block.
      • After undelete: This event is used when the record which has been sent to the Recycle Bin needs to be restored.

Example:
The following piece of code will help you understand how to create an object and how to create a trigger in Salesforce.

trigger tname on contact(before insert)
{ contact c = new contact();
if (c.email==null)
}

Watch this video on ‘Triggers in Salesforce.’ It will help you understand triggers better.

Triggers in Salesforce What Is Trigger in Salesforce? Triggers in Salesforce are called Apex Triggers. These are distinct and are available specifically for common and expected actions like lead conversions. Here are the topics you will learn in this section: What Is Trigger in Salesforce? Bulky Trigger Trigger Syntax Trigger Context Variables Triggers

Trigger Context Variables

All triggers in Salesforce determine implicit variables which enable developers to access the runtime context so that you don’t need to define objects from your side. The following table gives a list of context variables and their usage:

VARIABLEUSAGE
isInsertReturns true if this trigger was fired due to an insert operation
isUpdateReturns true if this trigger was fired due to an update operation
isDeleteReturns true if this trigger was fired due to a delete operation
isBeforeReturns true if this trigger has been fired before any record was saved
isAfterReturns true if this trigger was fired after all records have been saved
isUndeleteReturns true if this trigger was fired after a record has been recovered from the Recycle Bin
newReturns a list of new versions of the sObject records
newMapA map of IDs to the new versions of the sObject records
oldReturns a list of old versions of the sObject records
oldMapA map of IDs to the old versions of the sObject records
sizeThe total number of records in a trigger invocation, both old and new

All of these variables are given as: Trigger.variable
In the following piece of code, in the second line, you can see how the variable new is used as
Trigger.new. It used as a list of sObjects which can be iterated over in a for loop.
variable newCheck out the top Salesforce Interview Questions to learn what is expected from Salesforce professionals!

Triggers in Salesforce Vs. Workflow in Salesforce

We have learned about the workflow in one of the previous sections. And I’m sure you’d be confused between the workflow and triggers by now.
So, let me sort this out for you. Well, if Salesforce has created two different products, then it is obvious that there would be a huge difference between them. So, let’s discuss the same here:

Workflow:

  • It is an automated process that can shoot an action which is based on evaluation and rule criteria.
  • Performing DML operations in the workflow is not possible.
  • We can obtain a workflow over an object.
  • We can’t create a query from the database.

Trigger:

  • It is a piece of code which is executed either before or after a record is updated or inserted.
  • More than 15 DML operations can be used in a single trigger.
  • More than 20 SOQLs can be used from the database in a trigger.
  • We can access triggers across an object and related to that object.

Limitations of Workflows That Triggers in Salesforce Overcome:

  • Workflows are not able to create or update a separate object.
  • You can’t reference certain fields when using workflows.
  • You will not have your workflow doing more than just field updates and emails.

Learn more about Salesforce from this SalesforceTraining in Hyderabad to get ahead in your career!

Trigger Scenarios in Salesforce

We’ll now be talking about Trigger Scenarios in Salesforce.

There are the following three Trigger Scenarios in Salesforce:

Trigger Scenario 1

The following code will prevent users from creating a duplicate account, i.e., with the same names.

trigger AccountDuplicateTrigger on Account (before insert, before update)
   {
 for(Account a:Trigger.new)
   {
  List acc=“Select id from Account where Name=:a.Name and   Rating=:a.Rating“;
      if(acc.size()>0)
          {
          acc.Name.addError('You Cannot Create the Duplicate Account’);
          }
   }
   }

Trigger Scenario 2

This code will add a prefix ‘Dr.’ to all lead names whenever a record is updated or inserted.

trigger PrefixDoctor on Lead (before insert,before update)
    {
      List leadList = trigger.new;
         for(Lead l: leadList)
        {
              l.firstname = 'Dr.'+ l.firstname;
        }
    }

Trigger Scenario 3

This trigger will stop the users from deleting an account, as the System Administration has all permissions.

trigger AccountDelete on Account (before delete)
  {
     for(Account Acc:trigger.old)
       {
           acc.adderror('You Cannot Delete the Account Record’);
       }
  }

Still have queries? Come to Intellipaat’s Salesforce Community, clarify all your doubts, and excel in your career!

We have now come to the end of this session. I hope it helped you and has given you a very precise idea of what actually a trigger is in Salesforce. In the next session, we will be learning about
Get certified in Salesforce with Intellipaat’s Salesforce Certification Training!

Recommended Videos

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
15 + 25 =