Loading
Salesforce now sends email only from verified domains. Read More
Enhance Salesforce with Code
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Define Apex Triggers

          Define Apex Triggers

          Apex code can be invoked by using triggers. Apex triggers can be configured to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions.

          Required Editions

          Available in: Salesforce Classic (not available in all orgs) and Lightning Experience

          Available in: Performance, Unlimited, Developer, Enterprise, and Database.com Editions

          Standard Objects, Campaigns, Cases, and Emails are not available in Database.com.

          User Permissions Needed
          To define Apex triggers: Author Apex

          Apex triggers are stored as metadata in the application under the object with which they are associated.

          You can add, edit, or delete Apex using the Salesforce user interface only in a Developer Edition organization, a Salesforce Enterprise Edition trial organization, or sandbox organization. In a Salesforce production organization, you can change Apex only by using the Metadata API deploy call, the Salesforce Extensions for Visual Studio Code, or the Ant Migration Tool. The Salesforce Extensions for Visual Studio Code and Ant Migration Tool are free resources provided by Salesforce to support its users and partners, but are not considered part of our Services for purposes of the Salesforce Main Services Agreement.

          1. From the object management settings for the object whose triggers you want to access, go to Triggers. For the Attachment, ContentDocument, and Note standard objects, you can’t create a trigger in the Salesforce user interface. For these objects, create a trigger using development tools, such as the Developer Console or the Salesforce extensions for Visual Studio Code. Alternatively, you can also use the Metadata API.
          2. In the Triggers list, click New.
          3. To specify the version of Apex and the API used with this trigger, click Version Settings.
            If your organization has installed managed packages from the AppExchange, you can also specify which version of each managed package to use with this trigger. Associate the trigger with the most recent version of Apex and the API and each managed package by using the default values for all versions. You can specify an older version of a managed package if you want to access components or functionality that differs from the most recent package version.
          4. Click Apex Trigger and select the Is Active checkbox if you want to compile and enable the trigger. Leave this checkbox deselected if you only want to store the code in your organization's metadata. This checkbox is selected by default.
          5. In the Body text box, enter the Apex for the trigger. A single trigger can be up to 1 million characters in length.

            To define a trigger, use the following syntax:

            trigger TriggerName on ObjectName (trigger_events) {
                                 code_block
                                 }

            where trigger_events can be a comma-separated list of one or more of the following events:

            • before insert
            • before update
            • before delete
            • after insert
            • after update
            • after delete
            • after undelete
            Note
            Note
            • A trigger invoked by an insert, delete, or update of a recurring event or recurring task results in a runtime error when the trigger is called in bulk from the Lightning PlatformAPI.
            • Suppose that you use an after-insert or after-update trigger to change ownership of leads, contacts, or opportunities. If you use the API to change record ownership, or if a Lightning Experience user changes a record’s owner, no email notification is sent. To send email notifications to a record’s new owner, set the triggerUserEmail property in DMLOptions to true.
          6. Click Save

          Triggers are stored with an isValid flag that is set to true as long as dependent metadata has not changed since the trigger was last compiled. If any changes are made to object names or fields that are used in the trigger, including superficial changes such as edits to an object or field description, the isValid flag is set to false until the Apex compiler reprocesses the code. Recompiling occurs when the trigger is next executed, or when a user resaves the trigger in metadata.

          If a lookup field references a record that has been deleted, Salesforce clears the value of the lookup field by default. Alternatively, you can choose to prevent records from being deleted if they’re in a lookup relationship.

          All classes and triggers must compile successfully, and every trigger must have some test coverage. You must have at least 75% of your Apex covered by unit tests before you can deploy your code to production environments. See Apex Unit Tests.

           
          Loading
          Salesforce Help | Article