Apex Triggers, A Beginners Guide

Starting to look at the development side of Salesforce? Or are you wanting to just top up your knowledge on the simpler side of triggers? The basics of Apex Triggers are fairly easy to get your head around, it’s a little like baking a cake; once you understand the basics, you can replicate it time […]

0

Starting to look at the development side of Salesforce? Or are you wanting to just top up your knowledge on the simpler side of triggers?

The basics of Apex Triggers are fairly easy to get your head around, it’s a little like baking a cake; once you understand the basics, you can replicate it time & time again. But things can get complicated… quick. Luckily we’re only touching base with the basics today so you have nothing to worry about!

What Is An Apex Trigger?

Apex Triggers are essentially a way to perform an action after a database event has taken place (or before it has taken place). More accurately, Apex code is run after a certain Trigger occurs.

There are 2 main types of Triggers,

  • Before Triggers, &
  • After Triggers

They both do pretty much what it says on the tin, they either trigger Apex code just before a record is saved to the database or just after.

Going on to the 2nd main part of a Trigger is the event itself. There are 7 types of Trigger events you are able to use depending on the situation:

  • before insert,
  • before update,
  • before delete,
  • after insert,
  • after update,
  • after delete, &
  • after undelete.

Again, these pretty much do what they say on the tin and cover every possible type of action that can be performed on the database.

Apex Trigger Best Practices

These are just a few of many, to learn more about Apex best practices, you can reference Salesforce Developer Docs HERE.

  1. One Trigger Per Object,
  2. Bulkify your Code,
  3. Avoid Complex Logic in Triggers, &
  4. Use a Consistent Naming Convention.

Now, what’s a good article without some examples?

Pre-Example: Trigger Syntax
Trigger triggerName on sObject(Trigger event)
{
   //Custom Apex code goes here
}
The Example: When a Contact is created, set the ‘isNew’ checkbox to true
Trigger newContact on Contact(after insert){
   for(Contact contact : trigger.New){
       contact.isNew__c = true;
   }
}

You’ll notice in the example we have added a little more code compared to the syntax example we gave you, and it’s for good reason too. Referring back to Point #2 in the Best Practices list, I tell you to bulkify your Triggers. There are many reasons why you need to bulkify your Triggers, but the key reason being governor limits.

I won’t touch on them in this article, but if you are going to take 1 thing away from it… stay away from governor limits!

Summary

Now calm yourself down, don’t go rushing into creating Apex Triggers as Salesforce allows you to solve most business issues declaratively through the use of Flow and other automation tools. It’s actually best practice to use Apex as a last resort (apart from a few use cases where Apex is the only way), for those issues that cannot be solved any other way.

Cameron Ofoluwa
WRITTEN BY

Cameron Ofoluwa

22 Year Old Salesforce Developer @ Pogust Goodhead & Founder of SFDXHours.

Leave a Reply

Your email address will not be published. Required fields are marked *