Press enter to see results or esc to cancel.

What are Scratch Orgs in salesforce DX?

In the previous post we have learned about what is salesforce dx and how to set up salesforce cli. In this blog post we will learn how to create project and scratch orgs and how to use them.

By the definition A scratch Orgs are temporary Salesforce Orgs where developers will build source code and configure an application.

To create project in salesforce dx using cli execute below command

sfdx force:project:create --projectname SampleDXProject --outputdir C:\Users\sreddyk\Desktop\SFDXLearning\myWork

Here sampleDXProject is the folder name of the Project. Once you have the project created, you will see the folder structure, as shown in the following screenshot:

 

The configuration in the project-scratch-def.json file defines the structure of the scratch Org. Let’s look at the following sample project-scratch-def.json file:

{
"orgName": "Demo Company",
"edition": "Developer",
"orgPreferences" : {
"enabled": ["S1DesktopEnabled"]
}
}

You can find more about scratch org definition here

Note: There is only required field in the project-scratch-def.json file i.e edition remaining all others are not mandatory.

I have changed some of the definitions in project-sctarch-def.jason file as below.

{
"orgName": "Salesforce Developer",
"country": "US",
"username":"Username of your preference for scratch org",
"adminEmail":"Your admin email here",
"edition": "Enterprise",
"hasSampleData": "true",
"features": ["MultiCurrency"],
"orgPreferences": {
"enabled": ["S1DesktopEnabled", "ChatterEnabled"]
}
}

Execute below command to create scratch org.

scrtachOrg-Creation

Scratch org successfully created with username sreddyk+sfdx@salesforce.com

Note: You can always change the commands to meet your requirements. like you can make current scratch org as your default org etc.. Please find the documentation here.

Scratch-Org-Alias-Set

Now we are all set, created a scratch org and made it as default scratch org. Now lets open Scratch org and check …. Wait do we have credentials to open scratch org??? Oops.. We don’t have password with us, so what now how can we open scratch org? Here is the beauty of scratch Orgs they don’t require password to open. Its all in the commands.

Execute below command top open scratch org.

sfdx force:org:open -- If we made default org then this command is useful 
or
sfdx force:org:open -u myScratchOrg -- Here myScratchOrg is the Alias for my scratch org

scratch-org-open

We have created scratch org and learned how to open scratch org and keep in mind that you can generate password using below command

sfdx force:user:password:generate -u sreddyk+sfdx@salesforce.com 

OR you can see the password again by executing below command 

sfdx force:user:display -u sreddyk+sfdx@salesforce.com

Note : At a time of writing this post, scratch orgs auto deleted after 7 days however you can change the number of days when creating scratch org itself by mentioning days. The default is 7 days. Valid values are from 1-30.

sfdx force:org:create -f config/project-scratch-def.json --durationdays 30

So overall you can use this below command

sfdx force:org:create --setdefaultusername -f C:\Users\sreddyk\Desktop\SFDXLearning\myWork\SampleDXProject\config\project-scratch-def.json --setalias my_scratch_org --durationdays 30

Scratch_org_new

That’s all folks, We have created a new scratch org with expiration days 30 and default username set to newly created scratch org.

In the next blog post we will learn how to work with metadata changes from Scratch Org or Pull changes

Note: Please refer to this salesforce official documentation for more information.