Apex Triggers: A Comprehensive Guide

Salesforce's Apex triggers are an essential tool for developers and administrators, as they provide powerful and flexible customization to the platform's functionality.

This article aims to provide a comprehensive guide on Apex triggers, covering their types, creation, and adaptation for bulk operations. It will also address common queries surrounding Apex triggers and provide examples of their various use cases

Apex Triggers in Salesforce

What is an Apex Trigger?

An Apex trigger is a snippet of code in Salesforce that executes in response to specific events, such as the creation, modification, or deletion of records. These events are often referred to as triggers points. When a trigger point is reached, the associated Apex trigger fires, allowing developers to implement custom logic and actions. This enables you to automate processes, enforce business rules, and maintain data integrity seamlessly.

Here are some scenarios in which you could use an Apex Trigger:

Types of Apex Triggers

There are two types of Apex triggers:

Let's explore two Apex Trigger examples, one "Before Update" trigger and one "After Update" trigger to better understand their functionalities.

Before Insert Trigger

This Trigger example in Salesforce will be fired before an Account is created, and the logic will automatically populate the YearStarted field of the Account with the current year.

trigger UpdateYearStarted on Account (before insert) {
    for(Account acc : Trigger.new) {
        acc.YearStarted = System.today().year();
    }
}

Note that we are not using a DML operation (such as insert, update or upsert) because this is a Before trigger. The logic inside the trigger will be executed before the data is stored, so there is no need to perform a DML operation to store the record.

After Update Trigger

This trigger will be fired when someone updates the field: Industry of an Account. It will create a new Opportunity record.

trigger CreateOpportunityOnIndustryChange on Account (after update) {
    List<Opportunity> newOpportunities = new List<Opportunity>();

    for (Account acc : Trigger.new) {
        if (acc.Industry != null) {
            Opportunity newOpp = new Opportunity(
                AccountId = acc.Id,
                Name = 'New Opportunity ' + acc.Name,
                StageName = 'Prospecting',
                CloseDate = System.today().addDays(30)
            );
            newOpportunities.add(newOpp);
        }
    }

    if (!newOpportunities.isEmpty()) {
        insert newOpportunities;
    }
}

In this example, we must use a DML operation because we are creating a new record after the Account is saved into the database.

All types of trigger in Salesforce

Create an Apex Trigger

Creating an Apex trigger involves a series of steps:

  1. Navigate to Developer Console: Open the Developer Console from the setup menu.

  2. Create New Trigger: Click on "File" > "New" > "Apex Trigger" and provide a suitable name and object for the trigger.

  3. Add Trigger Logic: Implement your desired logic within the trigger's body. This can include conditional statements, database operations, and method calls.

  4. Save the Trigger: Once your code is ready, save the trigger.

Adapting Triggers to Bulk Operations

To bulk apex triggers efficiently, consider the following tips:

For more information on best practices in Apex triggers, as well as more examples, please refer to our article about Best Practices on Triggers. The article covers topics such as when to use a trigger, how to write a trigger, and common mistakes to avoid. It also includes a number of examples of how to use triggers to implement common business logic.

Answering Common Questions

How do I find Apex Triggers in Salesforce?

Can we call Apex from a Trigger?

Yes, you can invoke Apex classes and methods from triggers. This is considered a best practice because it allows you to separate the complex logic from the trigger.Here are some examples of how to do it

How do I get rid of an Apex Trigger?

To delete a trigger, navigate to "Setup" > "Apex Triggers" and click on the "Del" button next to the trigger you want to delete.

Can we deactivate the trigger?

Yes, you can deactivate a trigger by navigating to "Setup" > "Apex Triggers" > "Edit" and clicking on the is active checkbox.

Latest Articles: