What is Scratch Org?
In simple words, Salesforce Scratch org is a ‘use and throw’ org that we can use for the development and deployment of the Salesforce code and metadata. You can create the structure of scratch org however you want as it is fully configurable like you can emulate different Salesforce Edition or features and preferences. The best thing about your configuration is that you can configure a scratch org as you want and you can share the config file with others as well so that all of your team members are on the same page.
How are they different from Sandbox
- The main and important difference between the Salesforce Scratch Orgs and Salesforce Sandboxes is that the Scratch Orgs are disposable whereas the sandboxes are not disposable and need refreshing from time to time. This means that the scratch org has a specific life span, which could be anything between 1 and 30 days, depending on what you’ve set at the time of the creation.
- Other than this, scratch org takes the data from a configurable file whereas the sandbox copies the metadata from our Salesforce Environment, where Salesforce Environment could be either a Production org or any other sandbox.
- At the time of creation, scratch orgs don’t have any data but some types of sandbox copy the data from Production.
There are plenty of differences between a scratch org and a sandbox but let us stick to the above-mentioned for now. You can always use google for more if you really wanna dive a little deeper.
How to create Scratch Orgs
Scratch Orgs sits under your main Environment, whether it’s a Developer org or a Production org. Limits of these scratch org vary based upon the salesforce edition. To know the exact limits, follow this link. For the demo, we will be using a Developer Edition/Trial Org.
Enable Dev Hub
In order to create scratch org, we need to enable the Dev Hub feature in our salesforce org. Goto Setup and type ‘dev hub’. Click on ‘Dev Hub’ and enable the Dev Hub.
Once you enable it, go to VS code and create a new Project with Manifest.
NOTE: If you are new to VS code and SFDX, I would suggest you follow these two trails, Git and GitHub Basics and Quick Start: Visual Studio Code for Salesforce Development.
Once the Project is created, Press CTRL + SHIFT +P from your keyboard in VS code and click on SFDX: Authorize a Dev Hub.
NOTE: In order to run these commands, install Salesforce Extension Pack in your VS code. As you are here reading a salesforce article, I am assuming that you’ve already done it. If not, follow Set Up Visual Studio Code.
After you click on SFDX: Authorize a Dev Hub, you’ll be redirected to a login page. Enter your credentials of the org where you enabled the Dev Hub. Once done, you’ll see org added in your VS code.
Now we have our Dev Hub authenticated. Now onto the creation of Scratch Org.
Press CTRL + SHIFT +P from your keyboard in VS code and click on SFDX: Create a Default Scratch Org.
After that, it asks you to select the definition/configuration file path. You can have your configuration file or like in this case, use the default one. Click on the default file as shown in the below screenshot.
NOTE: If you want to know what a Scratch Org definition file is, check the article Scratch Org Definition File and its Customization
Give an alias to your scratch org and in the next, you’ll give the expiration days of this scratch org. By default, it’s 7 days but you can set it to anything between 1-30 days. Enter anything between 1-30 and press enter.
After a minute or so, you’ll see the creation operation successful and automatically selected in your VS code.
So, now scratch org is created, and if you wish to open this scratch org, press CTRL + SHIFT +P from your keyboard in VS code and click on SFDX: Open Default Org and it will open the scratch org in the browser.
Scratch Org Credentials
If for some reason, you need the credentials of this Scratch Org, or you’re coming to this article from Salesforce DevOps Center- Quick Setup Example, go to the terminal of VS code and type the below command:
sfdx force:user:password:generate --targetusername <username>
Where <username> is the Username of your user in the scratch org, which you can get when you open the scratch org as default from VS code. The output of this command will generate you a password, see the below screenshot:
Use test.salesforce.com to login into the scratch org and not login.salesforce.com.
Things to remember
Now as you know how to create Scratch Orgs, you’ll need to understand when to use Scratch Org in comparison to Sandboxed. Scratch Orgs should be your part of development, that’s for sure but only at the beginning of the Project. Let’s say you want to start the POC of a code and see if it works properly with your salesforce edition, independent of your metadata or code. Sandbox should come later in the development as the scratch org has a short life span.
The next thing to know is how you can configure your scratch org definition file. For this, I will soon write a detailed tutorial and will update it here. Until then, happy coding, and may the force be with you.