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.
-
Open a terminal (macOS and Linux) or command prompt Windows and change to your Salesforce DX project directory.
-
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/triggersdirectory if it doesn’t exist. -
Generate the Apex trigger; specify the trigger name with the
--nameflag and thetriggersdirectory with the--output-dirflag.By default, the generated trigger is for
before insertevents on the generic sObject. Use the--eventand--sobjectflags to change these default values. This example generates a trigger that fires before and after an insert into the Account object.
The command generates two files.
myTrigger.trigger-meta.xml—metadata filemyTrigger.trigger—Apex trigger source file
Use the project deploy start command to deploy the new Apex trigger to your org.
See Also