SFDX: How to setup a Scratch Org
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.
sf project generate -n YOUR_PROJECT_NAMEReplaces the deprecated sfdx force:project:create command.
Auth your DevHub
Next, we need to connect your DevHub with your new project
sf org login web -d -r https://login.salesforce.com -a ALIAS_FOR_YOUR_DEV_HUBReplaces the deprecated sfdx force:auth:web:login command.
-dsets this as the default Dev Hub.-rsets the login URL for the org.-asets this alias for the org.
If you have already auth’d, set your default username using
sf config set defaultdevhubusername=lukesfakeemail@force.com
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
sf org login web -r https://test.salesforce.com -a ALIAS_FOR_YOUR_SANDBOXReplaces the deprecated sfdx force:auth:web:login command.
Remember, don’t use the
-dflag. 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:loginsf org login web isnt working, use sfdx force:auth:device:loginsf org login device instead.
Rename (add) Alias
sf alias set NEW_ALIAS_FOR_YOUR_SANDBOX=current@sandbox.user.comsf 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
sf org logout -o ALIAS_FOR_YOUR_SANDBOXReplaces 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.jsonfile before progressing
sf org create scratch -v ALIAS_OF_YOUR_DEBHUB -f config/project-scratch-def.json -a ALIAS_FOR_SCRATCH_ORG -y 30 -w 10Replaces the deprecated sfdx force:org:create command.
-voptional param to choose your DevHub (not needed if you have a default DevHub set)-ssets this as the default sratch org-fsets the location for the config file (to build the org)-asets the alias for the scratch org-ysets the expiry to 30 days-wsets the wait time to 10mins-eedition to use (developer, enterprise, group, professional, partner-developer, partner-enterprise, partner-group, partner-professional)
View Scratch Org Config/Details
sf org display -o SCRATCH_ORG_ALIASReplaces the deprecated sfdx force:org:display command.
Generate Password Scratch Org
sf force user password generate -u SCRATCH_ORG_ALIASReplaces the deprecated sfdx force:user:password:generate command.
Delete Scratch Org
sf org delete scratch -o SCRATCH_ORG_ALIASReplaces 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.
sf org assign permset -n NAME_OF_PERMISSION_SETReplaces the deprecated sfdx force:user:permset:assign command.
most likely named
SalesConsoleUseron default scratch orgs
Deploy code back to DevHub
Deploy all of type
sf deploy metadata -m ApexPage, ApexClasses, LightningComponentBundle -o ALIAS_FOR_YOUR_DEV_HUBReplaces the deprecated sfdx force:source:deploy command.
Deploy specific component by path
sf deploy metadata -p force-app/main/default/lwc/SINGLE_COMPONENT_NAME -o ALIAS_FOR_YOUR_DEV_HUBReplaces 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
projectkeyword in the command.
Deploy all of type
sf project deploy metadata -m ApexPage, ApexClasses, LightningComponentBundle -o ALIAS_FOR_YOUR_DEV_HUBReplaces the deprecated sfdx force:source:deploy command.
Deploy specific component by path
sf project deploy metadata -p force-app/main/default/lwc/SINGLE_COMPONENT_NAME -o ALIAS_FOR_YOUR_DEV_HUBReplaces the deprecated sfdx force:source:deploy command.
Retrieve / Fetch / Pull Data
Retrieve all ApexClasses, ApexPages and LWC’s
sf project retrieve metadata -m ApexClass, ApexPage, LightningComponentBundle -o ALIAS_FOR_YOUR_DEV_HUBReplaces the deprecated sfdx force:source:retrieve command.
Create Data
Specify the Object type and the fields ‘n values
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_ALIASReplaces the deprecated sfdx force:data:record:create command.
Export Data
Using SQL to JSON data
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_ALIASReplaces the deprecated sfdx force:data:tree:export command.
Import Data
sf data import tree -f data/Account.jsonReplaces the deprecated sfdx force:data:tree:import command.
Create an Apex Class
sf apex generate class -n YourClassName -d force-app/main/default/classesReplaces 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.