Understanding Salesforce Triggers and working with them

Prakher Chaturvedi
4 min readMay 11, 2020

--

We are Pro now, with Apex, SOQL, and SOSL. If you are not, or you are not aware of what I am referring to jump to my previous story and BECOME PRO.

So, let’s discuss triggers in Salesforce, what are they, what do they do, and how can we write triggers.

TRIGGERS IN SALESFORCE :

A trigger is an automated action defined by Apex code which is executed when a DML event occurs on a record. In simple words, Trigger is an action, which is written in apex and is executed when some event occurs.

SYNTAX :
trigger Name on Object (event) {
code_block(actions to be performed)
}

So the syntax itself leaves a question mark? what is an event here?
So event is a DML Action which can be insert, update, delete.

What are the events that can cause a trigger to execute?
In the above syntax, one or more of the below-mentioned events can be used in place of event. Whenever we are using more than one event, events should be separated by a comma.

  • before insert
  • before update
  • before delete
  • after insert
  • after update
  • after delete
  • after undelete

TYPES OF TRIGGERS:

Having a look at the events mentioned above, you would have guessed what are the types of triggers. And if you have guessed it as before and after you are completely right. BINGO!

There are two types of triggers.
Before: used to update or validate record values before they’re saved to the database.
After: used to access field values that are set by the system and to affect changes in other records.

Trigger Context Variable:

All triggers define implicit variables that allow developers to access run-time context. These variables are contained in the System.Trigger class.
There are many context variables for trigger, we can understand their usage just by their name. Some of them are:
isExecuting, isInsert, isUpdate, isDelete, new, old, oldMap, size.

So as I mentioned earlier, the usage of these context variables can be understood by their name itself like isInsert will return true if the trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API. size will return the total number of records in a trigger invocation, both old and new.

trigger.new and trigger.old are the most frequently used trigger context variables.
trigger.new — Returns a list of the new versions of the sObject records.
This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.
trigger.old- Returns a list of the old versions of the sObject records.
This sObject list is only available in update and delete triggers.

I am now clear with the concept of a trigger, let’s jump on a trailhead module to get our hands dirty with some coding.
The Trailhead module which I will be taking up is Apex Triggers. Let’s get started.

Get Started with Apex Triggers:

We have to write a trigger for Account object, which will check for a checkbox field, and if it is set to true ie, it is checked the trigger has to set Shipping Address Postal Code equal to Billing Address Postal Code.
My Code:

Trigger Created on the Account object

Trigger named AccountAddressTrigger is created with events before insert and before update on Account object. We have used the Before type trigger here because the action performed by the trigger is to change the field of same record if a checkbox is checked by the user.

Match Billing Address is a custom check-box field created by us to execute this challenge, and if condition is used to mandate that trigger is executed only if check-box field is true.

Bulk Apex Triggers:

Apex triggers are optimized to operate in bulk. Whenever we use Bulk Apex triggers it has less chance to reach governor limits, provides better performance, and consumes less server resources.

In this challenge, We have to create a trigger on the opportunity object, that will create a task and adds it to any opportunity set to ‘Closed Won’.

Apex Bulk Trigger on Opportunity Object

Here, we are using after type trigger because the action performed by the trigger is to create a new task after this record is saved. In the above code, we are using list data-type for holding the object which is inserted at the last.
Basics of Bulk Triggers design define that we have to use fewer DML commands and we should avoid using DML commands in loops.

Once we have completed this module, we will be getting this shiny badge for the completion of this module.

Apex Triggers Completion Badge

So we are good to go with triggers now. We know when to use them and how to write them. Let’s take up, Apex test Classes and complete a trailhead badge alongside.

How Can You Contribute?

  • Connect with me on Twitter or Linkedin or Instagram.
  • Provide me continuous feedback and suggestions for moving ahead in this journey.
  • Share the series on Twitter and LinkedIn.

Posts In This Series :

Salesforce Development — Learning Apex Programming, VisualForce, Lightning Components
First Step toward Learning Apex Programming
Day 2 in Apex — Writing apex codes
Working with sObjects, SOQL, and SOSL
Understanding Salesforce Triggers and working with them
Apex Test classes and methods
Understanding VisualForce Concepts
Controllers in VisualForce

--

--

Prakher Chaturvedi

Looking for remote opportunities | Salesforce Developer | Lightning Web Components | APEX | 5x Certified | Trailhead Ranger | Content Creator (Medium)