Creating Scratch Orgs Using Visual Studio Code (Salesforce DX)

Scratch orgs are source-driven, transient environments that are entirely customizable.

These orgs represent disposable deployments of Salesforce code and metadata. They can be configured fully, enabling developers to mimic various Salesforce editions, complete with diverse features and preferences. Sharing the scratch org configuration file among team members ensures a consistent development environment for everyone involved.

Scratch orgs drive developer productivity and collaboration during the development process, and facilitate automated testing and continuous integration. You can use the CLI or IDE to open your scratch org in a browser without logging in. Spin up a new scratch org when you want to:

  • Start a new project.
  • Start a new feature branch.
  • Test a new feature.
  • Start automated testing.
  • Perform development tasks directly in an org.
  • Start from “scratch” with a fresh new org.

Supported Editions

The Salesforce edition of the scratch org. Possible values are:

  • Developer
  • Enterprise
  • Group
  • Professional

Scratch Org Allocations and Considerations

To ensure optimal performance, your Dev Hub org edition determines your scratch org allocations. These allocations determine how many scratch orgs you can create daily, and how many can be active at a given point. By default, Salesforce deletes scratch orgs and their associated ActiveScratchOrg records from your Dev Hub org when a scratch org expires. A scratch org expires in 7 days unless you set a duration when you create it.Scratch orgs have these storage limits:

  • 200 MB for data
  • 50 MB for files

To try out scratch orgs, sign up for a Developer Edition org on Salesforce Developers, then enable Dev Hub.

EditionActive Scratch Org AllocationDaily Scratch Org Allocation
Developer Edition or trial36
Enterprise Edition4080
Unlimited Edition100200
Performance Edition100200

Activate Dev Hub

In order to enable DevHub in your developer edition / trial / production org / business org, first need to enable Devhub.

in Setup → Devhub

To grant authorization to the Dev Hub

sfdx force:auth:web:login -d -a DevHub

Once you’ve enabled the devhub, you’ll have some objects automatically visible in your org like:- Active Scratch Orgs, Scratch Org Infos etc.
Active Scratch Org object basically consist of information about all the scratch orgs that are currently Active.Scratch Org Info object consist of information about all scratch orgs that you created using this DevHub no matter whether they’re currently active or expired. It basically have the metadata of your scratch orgs like:- Edition, Created Date, Expiration Date, Status etc.

Definition File for Scratch Org

The scratch org definition file is a blueprint for a scratch org. It mimics the shape of an org that you use in the development life cycle, such as sandbox, packaging, or production.

The settings and configuration options associated with a scratch org determine its shape, including:

  • Edition—The Salesforce edition of the scratch org, such as Developer, Enterprise, Group, or Professional.
  • Add-on features—Functionality that is not included by default in an edition, such as multi-currency.
  • Settings—Org and feature settings used to configure Salesforce products, such as Chatter and Communities.

By default, scratch orgs are empty. They don’t contain much of the sample metadata that you get when you sign up for an org, such as a Developer Edition org, the traditional way. Some of the things not included in a scratch org are:

  • Custom objects, fields, indexes, tabs, and entity definitions
  • Sample data
  • Sample Chatter feeds
  • Dashboards and reports
  • Workflows
  • Picklists
  • Profiles and permission sets
  • Apex classes, triggers, and pages

for more info, visit https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_scratch_orgs_def_file.htm

To view how many scratch orgs you have allocated, and how many you have remaining:

sfdx force:limits:api:display -u <Dev Hub username or alias>

Edition of the scratch org Supported features are listed in Scratch Org Definition Configuration Values → Supported EditionsFeatures to activate Supported features are listed in Scratch Org Definition Configuration Values → Supported FeaturesSettings to apply All supported settings in the Metadata API listed in Settings are supported in scratch org

Example Definition File

In the VS Code file explorer, open the config folder and click on the project-scratch-def.json file.

	
{
  "orgName": "jaya LWC",
  "edition": "Developer",
  "features": ["MaxApexCodeSize:10"],
  "settings" : {
      "orgPreferenceSettings": {
        "settings": {
            "orgPreferenceSettings": {
                "s1EncryptedStoragePref2": false
            },
            "securitySettings": {
                "sessionSettings": {
                    "sessionTimeout": "TwelveHours"
                },
             "mobileSettings": {
                "enableS1EncryptedStoragePref2": false
            }
        }
    }
   }
 }
}
  

orgName: Specifies the name for any scratch orgs generated using this definition. edition: Determines the initial template. Scratch orgs created from PBO Dev Hubs can leverage unique partner edition scratch orgs (e.g., Partner Developer and Partner Enterprise). features: Defines org features and, in certain cases, permits the configuration of “features with values,” potentially enabling specialized behaviors like increasing the Maximum Apex Code Size limit. s1EncryptedStoragePref2: Deactivates the caching of Lightning components, a helpful option for Lightning developers. sessionTimeout: Establishes a session timeout longer than 2 hours, facilitating ease in leaving an org and returning to work on it later in the day.

Generating a Scratch Org Using Visual Studio Code

Scratch orgs can last from 1 to 30 days

about:blank

Scratch org can be created using the Salesforce CLI/Visual Studio Code

sfdx force:org:create -f project-scratch-def.json -a MyScratchOrgAlias

you can push & pull metadatas to/from a scratch using following commands

source:push

 source:pull

 source:status

Process for Generating a Scratch Org

Use terminal in the VSCode and execute the following process.

Step 1: Establish a Project

Navigate to a directory to initiate a project. This project is intended for individual developers to work on assigned tasks within the project scope.

Use the shortcut Ctrl + Shift + P or access the command palette, then input ‘create project’. When operating with scratch orgs, it’s unnecessary to utilize ‘Create project with Manifest’.

Use Standard Template and Write project Name and click Enter again

Step 2: Authenticate the Directory (DEVhub)

write Below command in terminal to execute and open in your browser with salesforce dev org login page.

Authenticate with SFDX force: auth: web: login -d -a DevHub

The option -d designates the Default Dev Hub, configuring this DevHub as the default for future scratch org creations. The option -a assigns an Alias to your DevHub. An alias functions as a user-friendly name for your Dev Hub.

It’ll automatically open your browser where you can login and it’ll ask you for permissions to Allow. And you will be in your devhub org loggedin.

Step 3: Generate Scratch Org

To create Scratch org run below command in terminal.

Create a scratch org with the command: sfdx force:org:create -f config/project-scratch-def.json –durationdays 30 -a FirstScratch

(OR)

open the command palette and type Create a Default Scratch Org as shown below:-

it automatically creates a scratch org for the current project you’re working on. Choose this option and you’ll see another option where you need to select the scratch org definition file as given below:-

Choose the default scratch org definition file, already provided, unless you require additional features automatically activated in your scratch org, as previously set up.

You can access the sfdx-config.json file, where you’ll find the defaultdevhubusername and the defaultusername linked to the current project, illustrated below:

Note our scratch org name is “FirstScratch”. Your scratch org will be opened in the browser.

Step 4: Access your scratch org

Open the scratch org using: sfdx force:org:open -u FirstScratch

OR

Open Default Org and pressing Enter as shown below:-

This command will automatically open your newly created Scratch Org in your browser.