Salesforce Triggers Overview

Salesforce Triggers is a bunch of code that is executed based on record manipulation, results in the updation, insertion, or deletion of records or records. If any record is inserted, deleted, or updated, we can write a trigger and do some work with the code.

When working with a database, we will sometimes want to perform certain actions whenever a record is being changed against the database. For example, in the last chapter, we saw some example codes where we wanted to set the size of the company based upon the number of employees that the company had. We want this to occur every time a new account record is saved, and so is something we would place in a trigger. 

Triggers are special bundles of code that fire whenever an appropriate database action occurs. We can use triggers to allow us to define complex logical processes that occur whenever a record is changed against the Salesforce database. Triggers vs. Workflow/Process Builder Salesforce contains a number of different automation tools including Workflows and Process Builder that also fire on

Salesforce Triggers Types

Salesforce Triggers in Apex fire in a number of different contexts. Broadly speaking, I classify triggers into 2 contexts and 4 types. The 4 types of the trigger are as follows:

  • insert–runs when creating a new record. 
  • update–runs when updating an existing record. 
  • delete–runs when removing a record. 
  • undelete–runs when reinstating a record from the recycle bin. 
  • Upsert: This will run on Before and After only.

What is Before and After the event in Salesforce Triggers

Before and After is a condition for a trigger that decides when this trigger will execute.

  • If we are saying before insert this means, we are creating a record, and before it saves to database, we want this trigger to execute it. If we are using before insert trigger, we don’t need to do any DML operation on that record inside the trigger.
  • If we are saying after insert this means, we are creating a record, and after it saves to database, that is, we have a record ID now, and we want this trigger to execute it.

A trigger can either run in a before context or an after context. The before and after here refer to the record being saved against the database, and in the case of a record being inserted, it receiving an Id. The combination of these contexts and types gives us a flexible framework with which we can create and manage repeatable changes and logic that we want to apply to our records as they enter and move throughout various states within the system.

Salesforce Triggers syntax

Trigger <triggerName> on <ObjectName> (<events>)
  1. <triggerName>: You can write any name, but it’s best practice if you name it according to the work it will do, for example, updatePhoneTrigger. 
  2. <ObjectName>: Any salesforce object on which, this trigger will execute. 
  3. <events>: On which event you want to execute trigger, for example, before insert, before the update, after the update, after undelete, before delete, and so on. 

Trigger.New: It always holds the value of a list of sObject on which trigger is written. At the time of the event, whatever is the data or record is there, it is always in Trigger.New. In the preceding code, you can see the code was written on Account, so was holding the records of Account, that is, Trigger.New = List<account> this time. 

Trigger.old: Suppose you are changing the phone number of an account. So, if a trigger is there and the event is updated, now Trigger.old will hold the value of that record before changes, and will hold the changed data. We will use Trigger.NewMap and trigger.OldMap also in the future. These maps contain the ID of record as a key and that whole record as a value. 

Trigger.addError: If we want a custom validation such as validation rule on a record, we can use trigger.addError in before trigger. Trigger always works in batch. It means a Trigger.New or trigger.old works on 200 records in one execution. So, we should write a trigger in such a way that no governor limit should exceed.

How to Write Salesforce Triggers

Salesforce Triggers can be written Salesforce Developer console or we can use VS Code. Following are the steps: 

  • Open your developer console
  • Go to File and then New.
  • In the dropdown list, the second option is Apex Trigger, click on that
Creating Salesforce Triggers

Enter Trigger name and select sObject from the list.

trigger UpdatePhone on Account (before insert) {
Creating Salesforce Triggers

Here you can see a “Is Active” check box, remember that sometimes you have to deactivate the trigger, so you need to uncheck that box. If this “is Active” checkbox is checked, a trigger is active.

Scenario 1

When an account is created or updated, we have to change the status to Active or Inactive based on the account type. If the type is a prospect, then the status will be active and the active checkbox will be true; otherwise, it should be inactive. Trigger will update

trigger updateType on Account (before insert,before update)
    ac.Status __c=’Active’;
    else ac.Status __c=’Inactive’;
    if(ac.Status __c==’Active’ && ac.Active__c!=true)
    ac.Active__c.addError(‘Active field should be checked);
    else if(ac.Status __c==’Inactive’ && ac.Active__c!=false)
    ac.Active__c.addError(‘Active field should be Unchecked);
  • In the first line, the trigger is working on the Account object of Salesforce and event as before insert before the update.
  • Then, a for loop is running, which is fetching all the new records from trigger.New in Account variable ac. It means any account record that is creating or updating; all are stored in trigger.New.
  • After that, it checks the if condition, that is, if the account type is” Prospect,” then this account will become active; otherwise, it is inactive.
  • Then in the next if condition, it will check if the account status is Active, but the Active field is not checked, then it will throw an error; otherwise, the else will work.
Scroll to Top