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 upsert. Specify that the command generate its output into the triggers directory with the -d parameter.
    1sfdx force:apex:trigger:create -n mytrigger -s Account -e 'before insert, after upsert' -d <app-dir>/main/default/triggers

    The command generates two files.

    • mytrigger.trigger-meta.xml—Metadata API source file
    • mytrigger.trigger—Apex source file
  3. Update the generated Apex and Metadata API source files with your code.