Create apex class and triggers from salesforce CLI

Creating apex class and apex triggers from salesforce cli is similar. here is some sameple code snippet to create it.

Create Apex class form Salesforce CLI

This command will create apex class file and its meta file. 

Ex: sample.cls and sample.cls-meta.xml files.

// It will create in cwd
sf apex generate class -n sample

// it will create class in classes folder
sf apex generate class -n sample -d force-app/main/default/classes

Create Apex Trigger form Salesforce CLI

This command will create apex trigger file and its meta file.
Ex: accountTrigger.trigger and accountTrigger.trigger-meta.xml files. 
// It will create in cwd
sf apex generate trigger -n accountTrigger

// it will create trigger on Account object and triggers folder
sf apex generate trigger -n accountTrigger -s Account -d force-app/main/default/triggers

Flags

  • -n : Name of the class
  • -d : output-directory 
  • -s : name of the sobject to create trigger 

Source:
https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_apex_commands_unified.htm#cli_reference_apex_generate_class_unified

Labels:
Join the conversation