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.
By default, the trigger is created in the directory from which you run the command. The
standard DX project template assumes you’ll create your Apex triggers in the
<package directory>/force-app/main/default/triggers
directory. To create triggers in this directory, change to it before running the command.
1sfdx force:apex:trigger:create -n mytriggerIf you’re in a different directory, indicate the -d parameter to specify the absolute or relative path to the directory where you want to save your Apex class files. If you don’t indicate an absolute or a relative path and the directory doesn’t exist, the CLI attempts to create it for you.
1sfdx force:apex:trigger:create -n mytrigger -d ../force-app/main/default/triggersGenerate a skeleton trigger file 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.
1sfdx force:apex:trigger:create -n mytrigger -s Account -e 'before insert,after insert' -d ../force-app/main/default/triggersThe command generates two files.
- mytrigger.trigger-meta.xml—metadata format
- mytrigger.trigger—Apex trigger source file