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 use Salesforce CLI to create Apex triggers in your local Salesforce DX project. The generated files live in a triggers directory in a package directory of your project.
  1. Open a terminal (macOS and Linux) or command prompt Windows and change to your Salesforce DX project directory.
  2. Create the triggers directory in the location you want to generate the Apex trigger. For example, if you want to generate it in the default package directory, create the force-app/main/default/triggers directory if it doesn’t exist.
  3. Generate the Apex trigger; specify the trigger name with the --name flag and the triggers directory with the --output-dir flag.
    sf apex generate trigger --name myTrigger --output-dir force-app/main/default/triggers
    By default, the generated trigger is for before insert events on the generic sObject. Use the --event and --sobject flags to change these default values. This example generates a trigger that fires before and after an insert into the Account object.
    sf apex generate trigger --name myTrigger --event 'before insert,after insert' --sobject Account --output-dir force-app/main/default/triggers

The command generates two files.

  • myTrigger.trigger-meta.xml—metadata file
  • myTrigger.trigger—Apex trigger source file

Use the project deploy start command to deploy the new Apex trigger to your org.

sf project deploy start --metadata ApexTrigger:myTrigger --target-org myscratch