Newer Version Available

This content describes an older version of this product. View Latest

Create an Apex Trigger

Use Apex triggers to perform custom actions before or after a change to a Salesforce record, such as an insertion, update, or deletion. You can create Apex triggers from the CLI.
  1. If the triggers directory doesn’t exist in <app-dir>/main/default, create it.
  2. Generate skeleton trigger files by executing force:apex:trigger:create.
    • Use the -s parameter to specify the sObject associated with this trigger, such as Account.
    • Use the -e parameter to specify the triggering events, such as before delete or after insert.
    • Specify that the command generate its output into the triggers directory with the -d parameter.

    The command generates two files.

    • mytrigger.trigger-meta.xml—metadata format
    • mytrigger.trigger—Apex source file
  3. Update the generated Apex and metadata format file with your code.