Custom Code Templates

Custom Code Templates

Overview

Want your own code to appear in source files when you create a metadata object such as an Apex class? You can now use custom templates to do just that.

Templates are essentially folders with files that contain your custom code. This git repo contains a collection of official Salesforce templates for metadata components. Simply clone this repo, keeping the same folder structure, then update relevant template files with your code. Remove the files that you don’t wish to override.

Note: Only updates made to the files listed here show up in source files in VS Code. There’s no such restriction when you use the CLI to specify templates for metadata objects.

Template Folder Default Template Files
apexclass DefaultApexClass.cls
_class.cls-meta.xml
apextrigger ApexTrigger.trigger
_trigger.trigger-meta.xml
Lightningapp DefaultLightningApp.app
DefaultLightningController.js
DefaultLightningCss.css
DefaultLightningHelper.js
DefaultLightningRenderer.js
DefaultLightningSVG.svg
DefaultLightningAuradoc.auradoc
auradefinitionbundle.app-meta.xml
Lightningcomponent aura/default/default.cmp
aura/default/default.cmp-meta.xml
aura/default/defaultController.js
aura/default/defaultHelper.js
aura/default/defaultRenderer.js
aura/default/default.svg
aura/default/default.design
aura/default/default.css
aura/default/default.auradoc
lwc/default/default.js
lwc/default/default.html
lwc/default/default.js-meta.xml
Lightningevent defaultLightningEvt.evt
auradefinitionbundle.evt-meta.xml
Lightninginterface DefaultLightningIntf.intf
auradefinitionbundle.intf-meta.xml
Lightningtest DefaultLightningTest.resource
Project Any file in the project folder
Staticresource empty.resource
empty.js
empty.json
empty.css
empty.txt
_staticresource.resource-meta.xml
visualforcecomponent DefaultVFComponent.component
_component.component-meta.xml
visualforcepage DefaultVFPage.page
_page.page-meta.xml

Clone the Repo

See GitHub documentation for instructions on how to clone repos.

Set Default Template Location

  1. Open the sfdx-config.json config file in your <project-folder>/.sfdx folder
  2. Add the parameter customOrgMetadataTemplates, and set its value to either a local directory or your cloned GitHub repository that contains your default templates. The GitHub URL can either point to the root folder that contains your templates, or a path to a subfolder on a branch in the repo that contains your templates. For example:
    {
    "customOrgMetadataTemplates": "https://github.com/mygithubacct/salesforcedx-templates"
    }
    

    Note: You can also use the CLI to set this parameter. See CLI Runtime Configuration Values for more information.

Create a Apex Class with Custom Code

  1. Clone the sample GitHub repo.
  2. Delete all folders except the apexclass folder. Also delete all the files except the DefaultApexClass.cls file from the apexclass folder.
  3. Edit the DefaultApexClass.cls file with your custom code:
public with sharing class <%= apiName %> {
    		public <%= apiName %>(String prop) {
			this.prop = prop;
    		}

		@AuraEnabled public String prop { get;set; }
	}

Note: If you edit the file locally, remember to push changes to your repo. You can also choose to make changes directly in your repo.

  1. Update the sfdx-config.json file to point to your cloned repo (or local directory).
  2. Run the SFDX: Create Apex Class command from the Command Palette.
  3. Enter ApexClass for filename.
  4. Accept the default directory location.
  5. Confirm that the ApexClass.cls file contains your custom code:
    public with sharing class ApexClass {
     		public ApexClass(String prop) {
             this.prop = prop;
     		}
    
         @AuraEnabled public String prop { get;set; }
     }
    
  6. Share your config file with your teammates so that you’re all using the same config setting.

Make an Update to a Template

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.

Feedback or Bugs | Edit this Article