Co-Pilot or Autopilot: How Will AI Change Sales Cycles? Join our Live Event >>

A Guide to Triggers in Salesforce

Get started with triggers in Salesforce

Sorry, your browser does not support inline SVG.

As companies begin to expand the number of ways in which they use Salesforce within their organization, they look for more in-depth and automated ways to take care of specific tasks in the system. And ever-expanding reliance on Salesforce means that manual tasks can become a huge slow down across your organization.

Apex triggers within Salesforce are designed to help you automate certain tasks. Apex triggers allow you to perform custom actions before and after events in Salesforce. These events can include things such as data insertions, updates to existing data, or deletions.

Let’s take a look at what Apex triggers are in Salesforce, how they are typically used, and why they may be a benefit to your organization. In addition, you’ll get recommendations on how to improve your data management workflow overcoming the limitations of Salesforce Inbox and getting the most from trigger functionality.

What are triggers in Salesforce?

A Trigger is a segment of Apex code which executes before or after inserting or modifying a Salesforce record based on the condition provided. There are different types of triggers based on the action going to be performed. They are Before Triggers and After Triggers. Triggers allow modification of another record of the same type or different type.

There are two different types of Apex triggers within Salesforce:

  • “Before” Apex Triggers. These are used to update or validate the value in a record before you save it to your Salesforce database.
  • “After” Apex Triggers. These are used to access the values contained within a record and use that value to make changes to other records in your Salesforce database. Unlike “Before” triggers, “After” triggers are read-only.

Both types of triggers are useful for managing records and executing additional actions after manipulating data within your Salesforce database.
Here is what an Apex Trigger script looks like:

Trigger trigger Name on sObject(Trigger event)
{
//logic
}

That is a default setting. They are able to process multiple different records simultaneously. Both triggers can handle single or bulk operations including data Imports, bulk API calls, and bulk actions.
Apex triggers can be executed after a number of different events are taken within the Salesforce system. Here is a list of trigger events in salesforce:

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

Triggers can be defined for any top-level standard objects including accounts, contacts, and custom objects. Those triggers are activated by default when created.

How do you create a trigger in Salesforce?

If you’ve never used Apex Triggers in the past, figuring out where to place the actual triggers within the Salesforce system can be a bit confusing.

Additionally, you don’t want to start by launching codes to your live system. If it is your first time using Apex Triggers, you’ll want to run some tests before deploying anything live.

There are two places where you can practice writing Apex Trigger codes in Salesforce without the changes affecting your live database:

1. Sandbox – A vast majority of people code their triggers here. Sandboxes are developer-friendly copies of your normal org and can be created at any time.
2. Developer Edition – A perfect place to practice if you don’t want to use sandboxes.

No matter what version you are using — your live Salesforce database, a sandbox, or the Developer Edition — you can access the “Triggers” section by navigating to:
Setup >> Custom Code >> Apex Triggers

Here you can view all of your existing Apex Triggers, but cannot create new triggers. In order to create a trigger you have to use the Developer Console. Let’s illustrate this process with some examples.

Salesforce trigger examples

  • Example #1 Trigger on contact

The simplest example of a trigger is a trigger on contact which will create a new Account record whenever a contact is created without an account.

In the Developer Console, click: File > New > Apex Trigger.

Name the trigger “WelcomeTrigger” and choose “Account” as the sObject.

The developer console will show the default code that all Apex Triggers show before you make changes. Instead, enter the following Apex Trigger script:

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

You can then save it and test whether the trigger is working by creating a contact and checking if it creates a new account. You can also add the account manually through the debug system. When complete, you should see “Welcome!” in the debug window.

  • Example #2: Mark Accounts in Specific Industries as ‘Hot’

Your business probably prioritizes accounts and prospects in certain industries. It makes sense. Industries that are growing or are a particularly good fit for your offer should receive more attention from your internal teams. But often, that means that someone at your organization has to keep an eye on new accounts to mark them appropriately in Salesforce.
This can be done automatically using Apex triggers.

In the example below, we show how you can use Apex Triggers to mark new accounts that are from the technology or finance industries as “hot” in the system, helping your sales team to prioritize those accounts.

trigger AccountRatingTrigger on Account (before insert, before update) {
for(Account account : Trigger.New) {
if (account.Industry != null &&
(account.Industry == ‘Technology’ || account.Industry == ‘Finance’)){
account.Rating = ‘Hot’;
  }
 }
}

This is a good example of a simple and effective way to identify priority accounts for salespeople within your Salesforce database.

How you can get more from Salesforce triggers with Revenue Inbox

As you can see, the primary role of triggers is to automate your data management and workflow in Salesforce. Triggers enable you to perform custom actions (for example, update an account) before or after changes to Salesforce records. But what if you’d like to set similar triggers on incoming and outgoing emails? As a competent, busy Salesforce user, you probably enabled Einstein Activity Capture to auto-log your emails from Outlook or Gmail to Salesforce, only to realize that Einstein Activity Capture and Apex Email Message/Task triggers do not work together.

The problem is that Emails captured by EAC are not stored as physical records in Salesforce. They are stored in AWS and shown as virtual records on the Timeline and in the Activities Analytics dashboard. They can’t be queried like regular core Salesforce Activities and, hence, triggers will not work on them. That means if you want to automatically update a contact with data from a recent email, you won’t be able to.

Here’s where Revenue Inbox comes into play. It does everything Salesforce Inbox used to, but stores the messages as Task and Email Message objects (allowing you to set triggers on incoming and outgoing messages). And that’s just the beginning, it can also take over automatic data capture for emails, events, tasks, and attachments, auto-creation of standard and custom objects, and two-way synchronization between your Outlook or Gmail calendars and Salesforce.

Using Revenue Inbox and Salesforce triggers together, you’ll quickly fix common data management issues in Salesforce Inbox and EAC:

  • Configure to match any workflow; work with any custom object or field; use Salesforce views and Apex rules.
  • Set up activity auto-log rules, triggering your unique Salesforce processes.
  • Get your Salesforce contacts auto-created and updated in a dedicated folder or category in Outlook or Gmail.
  • Set up triggers on task reminders so you don’t forget anything important.
  • Set triggers on incoming and outgoing messages in Outlook or Gmail updating the required Salesforce records in process.

Discover all the benefits of Revenue Grid in action

    Corporate Email *
    Choose your platform below: *
    Office 365
    Office365
    Office 365
    Gmail
    Gmail
    Gmail
    The field is required.
    By submitting your information to our website you agree to the terms outlined in our privacy policy and our terms and conditions.
    loader-rg-2 | Revenuegrid.com

    Thanks

    for your interest in exploring Revenue Grid. We'll get in touch soon.

    img-victoria-golovtseva-blog-author
    B2B SaaS Content Writer

    B2B SaaS Content Writer with 7+ years of experience, specializing in ghostwriting and creating authentic, captivating narratives that set clients apart.

    • • Skilled in SEO copywriting, content strategy, and content development
    • • Delivered impactful content for SaaS tech companies, boosting client engagement and online presence
    • • Built strong client relationships through dedication to capturing their unique voice and message
    close
    expand_less