DEV Community

Cover image for SFDX Good to know - Scratch Orgs (PART 1)
Luke Secomb
Luke Secomb

Posted on • Edited on

SFDX Good to know - Scratch Orgs (PART 1)

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

Preparation

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

Ok, now we are setup lets get into it. The first step is to create an (SF)DX project.

sfdx force:project:create -n YOUR_PROJECT_NAME


Auth your DevHub

Next, we need to connect our DevHub with the new project

sfdx force:auth:web:login -d -a ALIAS_FOR_YOUR_DEV_HUB

  • -d sets this as the default Dev Hub.
  • -a sets this alias for the org. (strongly recommended)

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

You may also choose to change the default https://test.salesforce.com url to a custom domain if you have set one up.

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.


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. Later in this series we will properly cover the cool things we can achieve using the project-scratch-def.json file.

sfdx force:org:create -s -f config/project-scratch-def.json -a ALIAS_FOR_SCRATCH_ORG -d 30 -w 10

  • -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


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

most likely named SalesConsoleUser on default scratch orgs


And there we have it, you've successfully setup a new SFDX Project and connected it to a scratch org. In part 2 we will check out how we can push


TL:DR Commands

create new sfdx project
sfdx force:project:create -n YOUR_PROJECT_NAME

auth a devhub
sfdx force:auth:web:login -d -a ALIAS_FOR_YOUR_DEV_HUB

login to a sandbox
sfdx force:auth:web:login -r https://test.salesforce.com -a ALIAS_FOR_YOUR_SANDBOX

create a new scratch org
sfdx force:org:create -s -f config/project-scratch-def.json -a ALIAS_FOR_SCRATCH_ORG -d 30 -w 10

assign a permission set
sfdx force:user:permset:assign -n SalesConsoleUser

Top comments (0)