Newer Version Available
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.
- If the triggers directory doesn’t exist in <app-dir>/main/default, create it.
-
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/triggersThe command generates two files.
- mytrigger.trigger-meta.xml—Metadata API source file
- mytrigger.trigger—Apex source file
- Update the generated Apex and Metadata API source files with your code.