Custom Code Templates
Use custom templates to quickly add your own code template to source files when you create a metadata object such as an Apex class in VS Code. Templates are files that contain your custom code. You can use custom templates to:
- Add default copyright information to new files in your project.
- Add default code to Aura or LWC to enable a new Apex class, and so on.
This git repo subdirectory contains a collection of official Salesforce templates for metadata components. Only updates made to the files listed in this directory show up in source files in VS Code. You can clone this subdirectory, or replicate it locally. You must keep the same folder structure. Update relevant template files with your code, and remove the files that you don’t wish to override.
You can store template files in a local directory, or in a GitHub project. Set customOrgMetadataTemplates
to your custom template location:
- Run
sfdx config:set customOrgMetadataTemplates=<GitHub repo or local template directory>
command from the terminal inside VS Code. - Run
sfdx config:list
to confirm that the configuration setting is updated.
The sfdx-config.json
config file in your <project-folder>/.sfdx
folder is updated with a new entry in the format, "customOrgMetadataTemplates": "<GitHub repo or local template directory>"
.
See CLI Runtime Configuration Values for more information about configuration settings.
sfdx config:set customOrgMetadataTemplates=https://github.com/vscodeuser/salesforcedx-templates/tree/main/src/templates/
sets thecustomOrgMetadataTemplates
configuration value to a directory on uservscodeuser
's GitHub repo.sfdx config:set customOrgMetadataTemplates=/Users/vscodeuser/CustomTemplateProject/MyCustomTemplates
sets thecustomOrgMetadataTemplates
configuration value to theMyCustomTemplates
directory on a local machine.
- Clone this git repo subdirectory.
- In your cloned repo, delete all folders except the folders that contain the templates you want to use.
- Make updates to the custom templates in your repo.
- Check that
customOrgMetadataTemplates
points to this repo.
The Salesforce Extensions require your templates to follow the exact folder structure and nomenclature as this repo. There’s no such restriction when you use the CLI to specify templates for metadata objects.
- Create a folder in your VS Code project directory to hold your custom templates. Name the folder something intuitive, for example "MyCustomTemplates".
- Carefully check folder and file names in this git repo, and create subfolders of the same names that contain the files you wish to customize. For example, create a subfolder named
lightningapp
and add a file namedDefaultLightningController.js
to customize the default lightning controller JavaScript file. Create a subfolder namedapexclass
and add a file namedDefaultApexClass.cls
to add custom code to an Apex class. - Make updates to your custom template files.
- Check that
customOrgMetadataTemplates
points to your custom templates directory.
VS Code downloads the template files locally (~/.sfdx/custom-templates
on macOS/Linux or %USERPROFILE%\.sfdx\custom-templates
on Windows) the first time the template repository is accessed. To use updated templates, clear the local cached files to download the template files again.
If your template location is on your machine, any changes that you make to the template are ready for use immediately after save. If you're not seeing an immediate change, refresh your VS Code window to clear the VS Code cache to then see the changes reflected.