• LOGIN
  • No products in the cart.

A Guide to Triggers in Salesforce

Objective:

Triggers in Salesforce are known as Apex Triggers. These are known to be distinct and are available for expected and common actions like lead conversions. In this article, we will discuss the triggers in Salesforce.

Types of Apex Triggers:

In general, there are two types of Apex Triggers:

  1. Before Triggers
  2. After Triggers

Before Trigger: In this type, a trigger in the Salesforce can be used to either validate or update the record values even before they are saved into the database; hence, before the trigger can validate the record first and save it. Few codes or criteria can be set to check the information or data before it gets ready to insert into the database.

After Trigger: In this type, it is used to access the field values that are set by the system and can affect any change in the record. Otherwise, the after trigger can change the value from the information or data inserted in a few other records.

What are Bulky Triggers?

In Salesforce, all triggers are, by default, known to be bulky triggers. The user can process various records at a given time. Also, the Bulky triggers can handle several operations and the updates in a single record like:

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

Related Tech Courses: Salesforce Administration Training

Trigger Syntax:

The trigger syntax is very simple. Now, let’s go through the basic syntax:

trigger triggerName on Objectname(trigger_events)

{

//code_block

}

  • Objectname is defined as the object on which the action requires to be performed well.
  • TriggerName is the name that a user wants to give to the trigger.
  • Trigger_events are known to be the comma-separated list of many events like:
  • Before update: When the user uses this event, the code will be executed before the latest record is updated in the object.
  • Before insert: When using this event, the code block can be executed before inserting a new record.
  • After insert: In this case, the code block gets executed initially, and then the record insertion is done.
  • Before delete: While using this event, the record gets deleted before the code block execution.
  • After update: In this update, the record updating is done after the code block execution.
  • After delete: In this event, the user is able to delete the record after the code block execution.
  • After undelete: This is the event that is used when the record is sent to the Recycle Bin to be restored.

Here is an example for you. The below piece of code will help the user know how to create the object and create the trigger in Salesforce.

Trigger Context Variables:

In Salesforce, all the triggers determine the implicit variables, enabling the developers to access the context of runtime so that they don’t require defining the objects from the side. Given below is the table that gives a list of context variables along with their usage:

Context Variable Usage
isUpdate If the trigger was fired due to an update operation, it returns true.
isInsert If the trigger was fired due to an insert operation, it returns true.
isBefore If the trigger has been fired before any record was saved, it returns true.
isDelete If the trigger was fired due to a delete operation, it returns true.
isUndelete If the trigger was fired after a record has been recovered from the Recycle Bin, it returns true.
isAfter If the trigger was fired after all records have been saved, it returns true.
newMap A map of IDs to the new versions of the sObject records.
new Returns a new version list of the sObject records.
oldMap IDs map to the old versions of the sObject records.
old Returns a list of sObject records’ old versions.
size Total number of records in a trigger invocation, both old and new.

All of the above variables are used as Trigger. Variable. In the below piece of code, especially in the second line, the user can see how the variable new can be used as the Trigger New. It offers a list of sOjects that are iterated over in a loop.

Trigger simpleTrigger on Account (after insert) {

for (Account a : Trigger.new) {

// Iterate over each sObject

}

// This single query finds every contact that is associated with any of the

// triggering accounts. Note that although Trigger. new is a collection of

// records, when used as a bind variable in a SOQL query, Apex automatically

// transforms the list of records into a list of corresponding Ids.

Contact[] cons = [SELECT LastName FROM Contact

WHERE AccountId IN : Trigger, new];

}

Related Blogs: Learning Salesforce Service Cloud

Triggers in Salesforce vs. Workflows in Salesforce:

You have learned about the Salesforce Workflows in the last section. When you are learning about the triggers, there is a chance for the user to get confused between triggers and workflows in Salesforce.

Let us now sort this for the user. If Salesforce has created two various products, it is obvious that there would be a big difference among them. Let us know the difference.

Salesforce Trigger:

  • It is known to be a piece of code that can be used to execute either before or after the record is inserted or updated.
  • In the Trigger, more than 20 SOQLs are used from the database.
  • More than 15 DML operations are used in one Trigger.
  • The user can access the triggers across the object that is related to the object.

Salesforce Workflow:

  • In the workflow, performing the operations of DML is not possible.
  • It can automate the process which is based on the rule and evaluation criteria.
  • From the database, the user cannot create a query.
  • The user can obtain the workflow from an object.

Limitations of Workflows in Salesforce Overcome:

  • Users can’t reference some fields while using workflows.
  • Workflow can’t update or create a separate object.
  • Users will not have the workflow while doing more than the emails and field updates.

Conclusion:

Do you have queries regarding Salesforce? Contact Gologica or call: 8296960414, get all your doubts clarified and accelerate your career. Get certified in Salesforce with Gologica’s Salesforce Certification Training!

GoLogica Technologies Private Limited  © 2019. All rights reserved.