Building out Salesforce Developer Experience (SFDX) Scratch Orgs
By: Zohra Anwar
Tags: Sfdx, Salesforce Developer, Salesforce

What are Scratch Orgs?

Scratch Orgs are a source-driven and disposable deployment of Salesforce code and metadata. Whereas Sandboxes are copies of production. Scratch Orgs are not permanent, they don’t include any production data, and they are not replacements for Sandboxes. Instead, they are complements of Sandboxes, great for temporary deployments, and are typically used for peer review as a way to get enhanced test coverage and automation.

Scratch Orgs vs Sandboxes

Scratch Orgs are automatically deleted after 7 days, increase Developer productivity, create easier team collaboration, and help with DevOps automation.

Pre-requisites to be completed

Visual Studio Code install Salesforce CI extension install Salesforce Extension Pack install Lightning Web Component extension install

How to create a Scratch Org?

Once the above extensions are installed, open the command prompt using (Cmnd + P on macOS) and run sfdx plugins:install salesforcedx@latest. This will help ensure the latest version of Salesforce is run. Once it’s been installed run sfdx update.

Create an SFDX Project

Open the command pallet in VS Code and type in sfdx: create project. You will be prompted to give a project name. Once you have done that, select the location where you wanted to save your project. After creating your project, Visual Studio Code will re-start with the project files and check if the force-app-default contains a folder named “lwc“ (Lightning Web Component). As for naming convention with LWC, always use camelCase.

Authorizing a Dev Hub Org

After creating your project, you will now need to authorize your Dev Hub Org by opening up the command pallet then finding and selecting sfdx: authorize a Dev Hub.

Enable Dev Hub

You can enable Dev Hub by logging into your Trailhead account, going to your profile (located in the upper right-hand corner), selecting the Salesforce-Hands-on-Orgs, and selecting one of the Orgs you would like to enable. It will open up a new tab, go to Setup, then in Quick Find type Dev Hub. Once in the Dev Hub page, enable Dev Hub.

Once Dev Hub is enabled, it cannot be disabled.

Create Default Scratch Org

After you enable the Dev Hub Org, the next step is to create the default Scratch Org. To do this, you will need to go to the command palette in Visual Studio Code and find and select sfdx: create a default Scratch Org.

Select Configuration file (automatically suggested by Visual Studio Code) Provide an alias name for the Org Provide the number of days that you want to use Scratch Org (maximum value is 30 Days).

Component Configuration File

This file is generated when an LWC (Lightning Web Component) is created and holds the meta-data values for the component. The configuration file ends with .xml.

This pushes it to the Org along with the other components in the file with the same naming convention. It also requires an API to be 48 and exposure changed to true.

Target tags designate the component to specific pages on Salesforce. You can be as specific as or as general as you want.

This Scratch Org is created in association with the Dev Hub Org that was enabled.

Push source to Org

After creating an LWC (Lightning Web Component), right-click on the screen and scroll down to where it says deploy source to org. If successful, there will be a message in the lower right-hand corner indicating it as such.

Congratulations on setting up your Scratch Org from Visual Studio Code to Salesforce! Please reach out with any questions!

Helpful sites

About Cervello, a Kearney company

Cervello, a Kearney company is a data and analytics consulting firm and part of Kearney, a leading management consulting firm. We help our leading clients win by offering unique expertise in data and analytics, and in the challenges associated with connecting data. We focus on performance management, customer and supplier relationships, and data monetization and products, serving functions from sales to finance. We are a Salesforce partner and help our clients implement, customize, and optimize the platform into the best solution for their needs.