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_NAME
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_HUB
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
sf org login web -r https://test.salesforce.com -a ALIAS_FOR_YOUR_SANDBOX
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
sf alias set NEW_ALIAS_FOR_YOUR_SANDBOX=[email protected]
sf alias set OLD_ALIAS_FOR_YOUR_SANDBOX=
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_SANDBOX
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
sf org create scratch -v ALIAS_OF_YOUR_DEBHUB -f config/project-scratch-def.json -a ALIAS_FOR_SCRATCH_ORG -y 30 -w 10
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
sf org display -o SCRATCH_ORG_ALIAS
sfdx force:org:display
command.
Generate Password Scratch Org
sf force user password generate -u SCRATCH_ORG_ALIAS
sfdx force:user:password:generate
command.
Delete Scratch Org
sf org delete scratch -o SCRATCH_ORG_ALIAS
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_SET
sfdx force:user:permset:assign
command.
most likely named
SalesConsoleUser
on default scratch orgs
Deploy code back to DevHub
Deploy all of type
sf deploy metadata -m ApexPage, ApexClasses, LightningComponentBundle -o ALIAS_FOR_YOUR_DEV_HUB
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_HUB
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
sf project deploy metadata -m ApexPage, ApexClasses, LightningComponentBundle -o ALIAS_FOR_YOUR_DEV_HUB
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_HUB
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_HUB
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_ALIAS
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_ALIAS
sfdx force:data:tree:export
command.
Import Data
sf data import tree -f data/Account.json
sfdx force:data:tree:import
command.
Create an Apex Class
sf apex generate class -n YourClassName -d force-app/main/default/classes
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.