Apply Today

Writing Salesforce Apex Triggers

apex salesforce trigger Jul 25, 2021
Zen In Tech
Writing Salesforce Apex Triggers
4:37
 

Article Assumptions:

If you found this article, we assume that you have knowledge of the Salesforce platform and the Apex language and want to dig deeper in customizing and automating the platform. For example, we will cover the basics of triggers that run when records are inserted into the database. We also assume you have a general understanding of programming concepts like OOP(Object Oriented Programing) and constructs like conditionals.

The Method

You will first try to read and understand the code example. Then we will give you a further explanation of the code and what it means. This will activate your brain to try to decipher what you are reading; then, when you have a better understanding, we will revisit this code later on in the article. Additionally, you can Play the Audio, read along, and allow your mind to process the information visually and auditorially.

 

What is an Apex Trigger?

An Apex trigger is code that responds to changes in the salesforce database and allows you to take further action in the system. You can use them to do things like automate the creation, modification, or deletion of records, execute calculations, or talk to other systems. Essentially they are rules and actions in salesforce. The immense potential is vast, as triggers are the best and most powerful ways to automate processes on the Salesforce platform.

Why use Triggers?

With so many declarative options on Salesforce, why use apex triggers? The answer is simple; power, control, and debugging. Tools like Workflow Rules, Process Builder, and Flow are amazing but fall short in fine-grain control and debugging capabilities.

How it works:

Apex Triggers listen to Database Events from Salesforce Objects/Tables. Therefore, any time the Salesforce database creates/inserts, updates, or deletes records, you can write code that acts when these events happen.

The 3 Parts of Every Rule-based System

I like to think that every stable system can be broken down into three states, a trinity of sorts. When it comes to rules-based systems like triggers, those states are Context/Event, Condition, and Action.

  • Given a Context and Event

  • When These Conditions are Met

  • Then Do this Action

Practical Example:

  • Given a Water Faucet (Context), Nob is Turned (Event)

  • When the nob for hot water is open (Condition)

  • Then heat and release hot water (Action)

Salesforce Example:

  • Given a Contact Object(Context), Inserts a new record(Event)

  • When the record has completed inserting (Condition)

  • Then update a Contact Object Field (Action)

Review Code Example:

 

Breaking it down in English

1.Context and Events

  • Name: The Trigger Name is (ContactTrigger)

  • Object: The Object that we are listening to (Contact)

  • Events: The event we are listening for is (Before Insert)

2. Conditions:

  • If a certain event is fired, an action can be performed (If Trigger.isInsert and Trigger.isBefore) are Attribute/Properties of the Trigger and if both are true the underlying code will execute

  • You can have many additional requirements after the event conditions are met.

3. Action:

  • Is done when the conditions are met we log a (System.debug('Action') to the system console

Events Deep Dive

Now that we have tapped into the surface of Triggers we must dig deeper to get a thorough understanding of how Events work in triggers.

Before Triggers: These events fire right before the change to the database completes. IE (insert, update, delete, undelete)

After Triggers: These events fire immediately after the change to the database completes. IE (insert, update, delete, undelete)

The events are passed as parameters to the trigger

Full list of Events

before insert, after insert, before update, after update, before delete, after delete, after undelete

The Trigger Object

I can infer that the trigger is a class behind the scenes because of its structure. With that said, we have access to the Class Properties/Attributes and Methods of the trigger. Furthermore, we have access to the list of new records, a map of old and new records for comparison when there is an update to a record. Lastly, we can check if the Trigger is Executing as well as how many records it is processing through the trigger size.

Trigger.new, Trigger.oldMap, Trigger.newMap Trigger.isExecuting, Trigger.size()

Final Example

Let’s bring it all together for this final example. See if you can understand what is happening in the code below. Also, notice the use of the Trigger.new Attribute and the Trigger.size() method in the code. Again Trigger new contains a list of records captured by the trigger, and size denotes the size of the list.

Conclusion

Triggers are a powerful and indispensable tool in the Salesforce automation toolbelt. When you need control and access across the system codebase, then Apex Triggers are the way to go. 

View our courses

Stay connected with news and updates!

Join our mailing list to receive the latest news and updates from our team.
Don't worry, your information will not be shared.

We hate SPAM. We will never sell your information, for any reason.