---
schema_version: "1"
id: 554a6e7b22860182c9d56b44f5cd2bce535a9732c7cc6785cf34ee9b6e7491c0
title: Define Apex Triggers
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/platform/264-0-0/code_define_trigger.md
canonical_url: https://help.salesforce.com/s/articleView?id=platform.code_define_trigger.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 10a036ae33dbd9d118b8fe27dc9585c01e825409ca214c7acd73f1ae425bda12
description: 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.
products:
  - Salesforce Platform
release: "264"
---
# 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

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition">
        <colgroup>
          <col>
        </colgroup>
        <tbody>
          <tr class="">
            <td class="slds-cell-wrap" style="vertical-align:top;">Available in: Salesforce Classic (<a href="https://help.salesforce.com/s/articleView?id=xcloud.overview_edition_lex_only.htm&amp;type=5&amp;language=en_US" target="_blank">not available in all
                orgs</a>) and Lightning Experience</td>

          </tr>
          <tr class="">
            <td class="slds-cell-wrap" style="vertical-align:top;">
              <p>Available in: <strong>Performance</strong>, <strong>Unlimited</strong>, <strong>Developer</strong>,
                <strong>Enterprise</strong>, and <strong>Database.com</strong> Editions
              </p>
              <p style="margin-bottom:0;">Standard Objects,
                Campaigns, Cases, and Emails are not available in
                <strong>Database.com</strong>.
              </p>
            </td>

          </tr>
        </tbody>
      </table> <table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d15106e85">User Permissions
              Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d15106e85 ">To define Apex triggers:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d15106e85 ">Author Apex</td>

          </tr>
        </tbody>
      </table>

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:
    
    ```java
    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](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-platform-extend_code-264-0-0-production-enus/789bc523-706b-4ae4-8fe9-be6322e38b6b/images/icon_note.png)
    
    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 Platform​ API.
    - 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](https://help.salesforce.com/articleView?id=code_run_tests.htm&type=5&language=en_US).

#### See Also

- [Manage Apex Triggers](https://help.salesforce.com/s/articleView?id=platform.code_manage_triggers.htm&release=264.0.0&type=5 "A trigger is Apex code that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted.")
- [View Apex Trigger Details](https://help.salesforce.com/s/articleView?id=platform.code_view_trigger.htm&release=264.0.0&type=5 "Apex triggers are stored as metadata in the application under the object with which they are associated.")
- [_Apex Developer Guide_](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm)
- [Manage Version Settings for Apex](https://help.salesforce.com/s/articleView?id=platform.code_version_settings_apex.htm&release=264.0.0&type=5 "To aid backwards-compatibility, Apex classes and triggers are stored with version settings for the Salesforce API and any referenced managed packages.")
- [Find Object Management Settings](https://help.salesforce.com/s/articleView?id=platform.extend_click_find_objectmgmt_parent.htm&type=5&language=en_US)