SFDX: How to setup a Scratch Org

(Originally published ) 4 min read

How the heck do I set up a scratch org using sfdx? Well, you’re in the right place.

Firstly, you will need to have SFDX (Salesforce CLI) installed and VS Code. You will also need your own Salesforce DevHub instance setup (need a hand with setting up your dev hub? check out this trailhead unit.

Create a Salesforce DX Project

The second step is to create a (SF)DX project.

Terminal window
sf project generate -n YOUR_PROJECT_NAME

Replaces the deprecated sfdx force:project:create command.

Auth your DevHub

Next, we need to connect your DevHub with your new project

Terminal window
sf org login web -d -r https://login.salesforce.com -a ALIAS_FOR_YOUR_DEV_HUB

Replaces the deprecated sfdx force:auth:web:login command.

  • -d sets this as the default Dev Hub.
  • -r sets the login URL for the org.
  • -a sets this alias for the org.

If you have already auth’d, set your default username using sf config set [email protected]

Login To Sandboxes

In addition to DebHubs, we can also connect to standard salesforce Sandboxes. This can be handy when it comes to pulling components into your scratch org

Terminal window
sf org login web -r https://test.salesforce.com -a ALIAS_FOR_YOUR_SANDBOX

Replaces the deprecated sfdx force:auth:web:login command.

Remember, don’t use the -d flag. If you do, the CLI thinks the org is your Dev Hub, and then you’ll see an error when you try to create a scratch org.

If force:auth:web:login sf org login web isnt working, use sfdx force:auth:device:login sf org login device instead.

Rename (add) Alias

Terminal window
sf alias set [email protected]
sf alias set OLD_ALIAS_FOR_YOUR_SANDBOX=

Replaces the deprecated sfdx force:alias:set command.

Logout of Sandboxes

logout/remove the sandbox from the sfdx force:org:list

Terminal window
sf org logout -o ALIAS_FOR_YOUR_SANDBOX

Replaces the deprecated sfdx force:auth:logout command.

Create your scratch org

Now for the fun part, creating your scratch org.

if you want to set the scratch org name, or adjust other config options, edit the ./config/project-scratch-def.json file before progressing

Terminal window
sf org create scratch -v ALIAS_OF_YOUR_DEBHUB -f config/project-scratch-def.json -a ALIAS_FOR_SCRATCH_ORG -y 30 -w 10

Replaces the deprecated sfdx force:org:create command.

  • -v optional param to choose your DevHub (not needed if you have a default DevHub set)
  • -s sets this as the default sratch org
  • -f sets the location for the config file (to build the org)
  • -a sets the alias for the scratch org
  • -y sets the expiry to 30 days
  • -w sets the wait time to 10mins
  • -e edition to use (developer, enterprise, group, professional, partner-developer, partner-enterprise, partner-group, partner-professional)

View Scratch Org Config/Details

Terminal window
sf org display -o SCRATCH_ORG_ALIAS

Replaces the deprecated sfdx force:org:display command.

Generate Password Scratch Org

Terminal window
sf force user password generate -u SCRATCH_ORG_ALIAS

Replaces the deprecated sfdx force:user:password:generate command.

Delete Scratch Org

Terminal window
sf org delete scratch -o SCRATCH_ORG_ALIAS

Replaces the deprecated sfdx force:org:delete command.

Assign Permission Set

Before you can start pushing code, we have to set up some permission sets to allow us.

Terminal window
sf org assign permset -n NAME_OF_PERMISSION_SET

Replaces the deprecated sfdx force:user:permset:assign command.

most likely named SalesConsoleUser on default scratch orgs

Deploy code back to DevHub

Deploy all of type

Terminal window
sf deploy metadata -m ApexPage, ApexClasses, LightningComponentBundle -o ALIAS_FOR_YOUR_DEV_HUB

Replaces the deprecated sfdx force:source:deploy command.

Deploy specific component by path

Terminal window
sf deploy metadata -p force-app/main/default/lwc/SINGLE_COMPONENT_NAME -o ALIAS_FOR_YOUR_DEV_HUB

Replaces the deprecated sfdx force:source:deploy command.

Deploy code to a Sandbox

NOTE: These are the same deploy commands as above but with the extra project keyword in the command.

Deploy all of type

Terminal window
sf project deploy metadata -m ApexPage, ApexClasses, LightningComponentBundle -o ALIAS_FOR_YOUR_DEV_HUB

Replaces the deprecated sfdx force:source:deploy command.

Deploy specific component by path

Terminal window
sf project deploy metadata -p force-app/main/default/lwc/SINGLE_COMPONENT_NAME -o ALIAS_FOR_YOUR_DEV_HUB

Replaces the deprecated sfdx force:source:deploy command.

Retrieve / Fetch / Pull Data

Retrieve all ApexClasses, ApexPages and LWC’s

Terminal window
sf project retrieve metadata -m ApexClass, ApexPage, LightningComponentBundle -o ALIAS_FOR_YOUR_DEV_HUB

Replaces the deprecated sfdx force:source:retrieve command.

Metadata Ref

Create Data

Specify the Object type and the fields ‘n values

Terminal window
sf data create record -s Account -v "Name='Marriott Marquis' BillingStreet='780 Mission St' BillingCity='San Francisco' BillingState='CA' BillingPostalCode='94103' Phone='(415) 896-1600' Website='www.marriott.com'" -o SCRATCH_ORG_ALIAS

Replaces the deprecated sfdx force:data:record:create command.

Export Data

Using SQL to JSON data

Terminal window
sf data export tree -q "SELECT Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, Phone, Website FROM Account WHERE BillingStreet != NULL AND BillingCity != NULL and BillingState != NULL" -d ./data -o SCRATCH_ORG_ALIAS

Replaces the deprecated sfdx force:data:tree:export command.

Import Data

Terminal window
sf data import tree -f data/Account.json

Replaces the deprecated sfdx force:data:tree:import command.

Create an Apex Class

Terminal window
sf apex generate class -n YourClassName -d force-app/main/default/classes

Replaces the deprecated sfdx force:apex:class:create command.

config/project-scratch-def.json

Disable Lightning Experience caching

"settings": {
"orgPreferenceSettings": {
"s1EncryptedStoragePref2": false
}
}

Disabling secure and persistent browser caching has a significant negative performance impact on Lightning Experience. Always enable the setting in production orgs.