SaasGuru Logo

Triggers in Salesforce – An Ultimate Guide

Table of Contents

Triggers in Salesforce - An Ultimate Guide

Salesforce offers an ample amount of career opportunities as the requirements of Salesforce services steadily grow with the size of an organization.

When any business grows, complexity increases in daily operations. In that case, the demand for efficient automation becomes the need of the hour for complex tasks in the organization.

It can slow down all business processes if organizations keep using existing Salesforce services without automation. The manual operation does the job for smaller tasks, but when businesses grow bigger, processes and tasks tend to become more complex. To handle that, it is advisable to have automation in place.

This is where the Apex Triggers in Salesforce come into play. Apex Triggers help automate complex tasks in Salesforce.

Apex Triggers are specifically designed to perform a complex series of actions in Salesforce, after and before some events like when a record is created, updated, or deleted. It turns out to be effective in data management as well.

As Triggers make many tasks easy, it is important for you to know about them and how they work. In this post, we will explore in detail how exactly Apex Triggers help businesses achieve the required degree of automation.

Let’s start with what is an Apex Trigger. It is an Apex code that works based on predefined conditions. When a record is updated, the Trigger executes its action before or after it is updated as per the condition set.

You can update records of the same object or multiple objects at once with Triggers. As there are different types of actions in operation, Triggers need to be different for that particular action. So, there are different types of Salesforce Triggers for that. Let’s learn what they are.

Become a Salesforce Certified Professional

Use Coupon Code BLOG20 to avail flat 20% discount on saasguru Programs.

What are the Different Types of Apex Triggers in Salesforce?

Broadly, there are 2 types of Triggers in Salesforce.

  • Before Triggers
  • After Triggers

Before Triggers help you update and validate values in a  record before committing it to the database. 

On the other hand, After Triggers help to fetch the values in the records. With After Trigger, you can utilize those values to make changes in other records. 

One more difference between the two is you can edit and update records with the help of before Triggers, but After Triggers are read-only.

Read about What are Apex Methods?

Syntax of Trigger

Syntax of Trigger

Here, you can give a name to your Apex Trigger in the Trigger name. In the object name, select any action that you intend to execute. And in the Trigger event, you can select any event from multiple options.

    • Before update: Executes the trigger before a record is updated
    • Before insert: Executes the trigger before a record is inserted
    • Before delete: Executes the trigger before a record is deleted
    • After delete: Executes the trigger after a record is deleted
    • After insert: Executes the trigger after a record is inserted
    • After update: Executes the trigger after a record is updated
    • After undelete: Executes the trigger to recover a deleted record from recycle bin

How to Create an Apex Trigger in Salesforce?

If you have never created Triggers before, it can be a bit confusing in which system you can create them, production or sandbox. Moreover, you would never want to experiment with creating Triggers in a production environment. You should use some test simulations before the actual creation and execution of the trigger in the live system.

A sandbox or a developer edition org is your go-to place to play around with triggers. No matter what apex code you run there, it doesn’t affect your live Salesforce database.

  • Sandbox is very convenient for developers. That’s why the majority of them prefer to spin a developer sandbox to code the Triggers in quickly. 
  • If you do not want to use a sandbox, then a developer edition is your best friend to help you practice.

Let’s create a simple trigger on the Account object. Follow the below steps to create a trigger:

  1. Click the “Setup” icon.
  2. Right-click on the “Developer Console” and click “Open link in New Tab.” This way, the developer console opens as a tab in the browser window rather than a new window. This is more convenient to work with.
  3.  Click “File” -> “New” -> “Apex Trigger”
  4. Specify “Name” and select “Object.”
  5. Click “Submit.” 

6. Then, you will see the syntax that we talked about earlier in this post. Now start writing your Apex Trigger. You will find the default code in the Apex Trigger. Replace that code with the following code.

trigger AccountTrigger on Account (before insert) {System.debug(‘Welcome’);}

7. Now, create an Account and check if the trigger is working or not. If it creates an account, then the trigger is working. You will find welcome in the debug window when it is completed.

Once you have created some triggers, where do you find them? Here are the steps that you can follow:

  1. Navigate to set up
  2. Type “apex triggers” in the quick find box and click “Apex Triggers” under the Custom code section.
  3. The apex Triggers page will open in setup showing you the list of Triggers created so far or the ones that are part of a managed package.

Triggers are generally used to execute actions that are not possible to perform with point-and-click tools in Salesforce. Let’s try to understand creating trigger with an example,

What are Trigger Exceptions?

Trigger exceptions are the restrictions that you sometimes need in the database operation. Trigger exceptions are also called Block Triggers. For example, Triggers can be used to prevent the occurrence of DML operations by using the addError() method on the field.

So when Triggers are used, custom error messages will be seen in the application interface. There is a minimum delay in response time if errors are added before the Triggers.

Triggers vs. Workflow in Salesforce

Triggers and Workflows are both automation tools in Salesforce. Triggers are programmatic, and Workflows are declarative in nature. So let’s not get confused between the two. Here are four major differences between an Apex Trigger and a Workflow Rule.

Apex Triggers

Read about What is Salesforce Vlocity?

Salesforce Trigger & Revenue Box

How can you get the maximum out of Salesforce Trigger with the Help of Revenue Box?

You can combine the Salesforce Trigger and Revenue Box to resolve some common data management issues in the Salesforce Inbox and Einstein activity capture (EAC).

As of now, you are aware that Triggers can automate complex tasks in your database and workflow. You can do the same with your incoming and outgoing emails with the help of the Triggers.

EAC in Salesforce helps auto-log your emails from Gmail and Outlook to your Salesforce. But EAC and Apex email task Triggers do not work together. Because EAC does not store physical records of the captured emails from Gmail or Outlook, they only show virtual activities and timelines on the record but are not stored physically.

This is the reason they are not considered a core Salesforce activity, so Triggers do not work for them. Hence you are unable to update Contacts in your database from your recent emails. This is where the Revenue Box helps you solve this problem.

Revenue Box works the same as Salesforce Inbox, but apart from that, it stores emails that enable you to create Triggers. Moreover, the Revenue Box can automate the data capture from the emails and facilitates active synchronization between Salesforce and email services like Outlook and Gmail.

Get your Salesforce Certifications on the first go

Use Coupon Code BLOG20 to avail flat 20% discount on saasguru Programs.

Recursive Trigger and Bulkifying Trigger

A Recursive Trigger is an event where the same Trigger is fired again due to the results generated by the trigger and creates an infinite loop. A Recursive Trigger must be avoided at any cost. If such an unfortunate event happens, then you will hit the Salesforce governor limits. To avoid a recursive trigger, you need to create a static variable and keep checking the value of that variable before any execution in the trigger.

The basic purpose of a trigger is to handle multiple records that can be hundreds or thousands. All those need to be handled efficiently. In this case, you can bulkify the Triggers.

To bulkify the Trigger, you should write a Trigger that can facilitate efficient DML and SOQL operations. Data operations may hit the upper limit if you do not perform this action. So, it is always a best practice to bulkify your triggers.

So, that’s it about Salesforce Triggers. We hope you have received so many insights and the applicability of Salesforce Triggers. If you still have any queries, do not hesitate to connect with us. We will be delighted to assist you.

Sign Up now to get information about more Salesforce features and concepts.

Get certified as a Salesforce Administrator in your first attempt with saasguru.

Looking for Career Upgrade?

Book a free counselling session with our Course Advisor.

By providing your contact details, you agree to our Terms of use & Privacy Policy

Related Articles

saasguru is Scaling Up with New Seed Funds of 4 Million AUD

saasguru has successfully raised a whopping amount of AUD 4 million in the second seed funding round led by Square Peg Capital on November 29, 2022. Read more.

How To Transition Into a Cloud Career Without Experience?

Explore ways to get a job in cloud computing without prior experience and how to use your certification to get a job in the cloud. Read now!

AWS Certification Types and Their Costs in India 2023

AWS certifications are divided into 4 tiers – the first being entry-level & the 4th being the most advanced. Explore AWS certifications & their costs in India here.

Scroll to Top