Skip to main content

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.

shell
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

shell
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

shell
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

shell
sf alias set NEW_ALIAS_FOR_YOUR_SANDBOX=[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

shell
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

shell
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

shell
sf org display -o SCRATCH_ORG_ALIAS
Replaces the deprecated sfdx force:org:display command.

Generate Password Scratch Org

shell
sf force user password generate -u SCRATCH_ORG_ALIAS
Replaces the deprecated sfdx force:user:password:generate command.

Delete Scratch Org

shell
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.

shell
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

shell
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

shell
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

shell
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

shell
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

shell
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

shell
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

shell
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

shell
sf data import tree -f data/Account.json
Replaces the deprecated sfdx force:data:tree:import command.

Create an Apex Class

shell
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

json
"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.