DEV Community

Cover image for SFDX: Good to know
Luke Secomb
Luke Secomb

Posted on • Edited on • Originally published at blog.lukesecomb.digital

SFDX: Good to know

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.

sfdx force:project:create -n YOUR_PROJECT_NAME
Enter fullscreen mode Exit fullscreen mode

Auth your DevHub

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

sfdx force:auth:web:login -d -a ALIAS_FOR_YOUR_DEV_HUB -r https://test.salesforce.com
Enter fullscreen mode Exit fullscreen mode
  • -d sets this as the default Dev Hub.
  • -a sets this alias for the org.
  • -r sets the login URL for the org.

If you have already auth'd, set your default username using sfdx force: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

sfdx force:auth:web:login -r https://test.salesforce.com -a ALIAS_FOR_YOUR_SANDBOX
Enter fullscreen mode Exit fullscreen mode

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 isnt working, use sfdx force:auth:device:login -r https://test.salesforce.com -a YOUR_ORG_ALIAS instead.

Rename (add) Alias

sfdx force:alias:set NEW_ALIAS_FOR_YOUR_SANDBOX=current@sandbox.user.com
sfdx force:alias:set OLD_ALIAS_FOR_YOUR_SANDBOX=
Enter fullscreen mode Exit fullscreen mode

Logout of Sandboxes

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

sfdx force:auth:logout -u ALIAS_FOR_YOUR_SANDBOX
Enter fullscreen mode Exit fullscreen mode

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

sfdx force:org:create -s -v ALIAS_OF_YOUR_DEBHUB -f config/project-scratch-def.json -a ALIAS_FOR_SCRATCH_ORG -d 30 -w 10
Enter fullscreen mode Exit fullscreen mode
  • -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
  • -d sets the expiry to 30 days
  • -w sets the wait time to 10mins

If you already scratch'd an org sfdx force:config:set defaultusername=lukesfakeemail@force.com

View Scratch Org Config/Details

sfdx force:org:display -u SCRATCH_ORG_ALIAS
Enter fullscreen mode Exit fullscreen mode

Generate Password Scratch Org

sfdx force:user:password:generate -u SCRATCH_ORG_ALIAS
Enter fullscreen mode Exit fullscreen mode

Delete Scratch Org

sfdx force:org:delete -u SCRATCH_ORG_ALIAS
Enter fullscreen mode Exit fullscreen mode

Assign Permission Set

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

sfdx force:user:permset:assign -n NAME_OF_PERMISSION_SET
Enter fullscreen mode Exit fullscreen mode

most likely named SalesConsoleUser on default scratch orgs

Fetch all Metadata from an Org (Metadata API)

NOTE: we are using the sfdx-ext plugin which can be found here

Fetch the Metadata

sfdx ext:mdapi:retrieve -b -i -n -h -f -u SOURCE_ORG_NAME
Enter fullscreen mode Exit fullscreen mode

Convert the source to Metadata API (instead of Source API)

sfdx ext:mdapi:convert --sourcedirectory src --targetdirectory ./
Enter fullscreen mode Exit fullscreen mode

Clean up the Source API folder

rm -rf src
Enter fullscreen mode Exit fullscreen mode

If any of this falls over, you may need to either update the sfdx-ext plugin or remove un-parsable files.

Deploy code back to DevHub

Deploy all of type

sfdx force:source:deploy -m ApexPage, ApexClasses, LightningComponentBundle -u ALIAS_FOR_YOUR_DEV_HUB
Enter fullscreen mode Exit fullscreen mode

Deploy specific component by path

sfdx force:source:deploy -p force-app/main/default/lwc/SINGLE_COMPONENT_NAME -u ALIAS_FOR_YOUR_DEV_HUB
Enter fullscreen mode Exit fullscreen mode

Fetch / Pull Data

Retrieve All ApexClasses, ApexPages, LWC's

sfdx force:source:retrieve -m ApexClass, ApexPage, LightningComponentBundle -u ALIAS_FOR_YOUR_DEV_HUB
Enter fullscreen mode Exit fullscreen mode

Metadata Ref

Create / Import Data

data can be retrieved from a sandbox using sfdx force:source:retrieve -m CustomObject -u SANDBOX_SOURCE_ORG

Create Data

Specify the Object type and the fields 'n values

sfdx force:data:record:create -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'"
Enter fullscreen mode Exit fullscreen mode

Export Data

Using SQL to JSON data

sfdx force:data:tree:export -q "SELECT Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, Phone, Website FROM Account WHERE BillingStreet != NULL AND BillingCity != NULL and BillingState != NULL\" -d ./data
Enter fullscreen mode Exit fullscreen mode

Import Data

sfdx force:data:tree:import --sobjecttreefiles data/Account.json
Enter fullscreen mode Exit fullscreen mode

Create an Apex Class

sfdx force:apex:class:create -n YourControllerName -d force-app/main/default/classes
Enter fullscreen mode Exit fullscreen mode

config/project-scratch-def.json

Disable Lightning Experience caching

"settings": {
  "orgPreferenceSettings": {
    "s1EncryptedStoragePref2": false
  }
}
Enter fullscreen mode Exit fullscreen mode

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

Useful Commands

  • See config options sfdx force:config:set -h
  • See all commands sfdx force:doc:commands:list
  • Refresh SObject cache for VS Code intellisense sfdx sobject definitions refresh -u SCRATCH_ORG_USERNAME

Top comments (0)