Understanding Salesforce Scratch Orgs
Myra Rafique Khan
Developer @ S&P Global | Python | 2x Salesforce Certified | Dedicated to digitally transform your business | Focused on data-driven solutions | Quick Learner | Resilient Problem Solver |
Revision of Salesforce Orgs
Let's quickly cover the different kinds of orgs we usually deal with when working with the Salesforce platform. If you have ever studied for a Salesforce certification exam, you would've come across a module explaining what each of these orgs is used for. This quick summary will help refresh that.?
Production Org
A production org is the final destination for your code and applications, and it’s the primary environment where you conduct your day-to-day business operations using Salesforce. It is recommended that you avoid modifying any code or metadata directly in production, which can introduce bugs or issues that can disrupt your business or your customers’ businesses.
Sandbox
A sandbox is an org that provides a safe space for developers and admins to experiment with new features and validate changes before deploying code to production. We have different types of sandboxes:
What are Salesforce Scratch Orgs?
Now, you might have already worked in the above kinds of orgs but none of these are what we call scratch orgs.?So, what exactly are scratch orgs then? Let's move on to that!
Salesforce scratch orgs are temporary environments specifically designed for development and testing purposes. By default, they do not contain sample data, workflows, profiles, permission sets, apex classes, triggers, or pages. In short, scratch orgs are completely empty and they don’t contain much of the sample metadata that you get when you sign up for an org, such as a Developer Edition org.
They are also fully configurable which means that developers can add different features and settings as they like. In addition to code and metadata, developers can also install packages and deploy synthetic or dummy data for testing.
So scratch orgs are for development and testing and we just talked about how different sandboxes are also created for development and testing purposes. Where exactly does the difference come in? Let’s look at that.
Scratch Org v/s Sandboxes
Hopefully, that explained the difference between a scratch org and a sandbox and gave you more understanding of scratch orgs. In summary, we say that they are isolated and disposable salesforce orgs used for development and testing which you can spin up in an instant and also get rid of them in minutes once you’re done with them.
Configuration Files
As we mentioned, scratch orgs are fully configurable. There is a file named project-scratch-def.json which makes all this configuration possible. It has the following elements:
Another example is the Security Settings where you can set password policies such as when it expires, and maximum login attempts.
Data Files
Additionally, if you want to import some dummy data into the scratch org, you can create JSON data files that look like the following screenshot:
领英推荐
In type, you write down which object will have the following record. After that, you write the field names on the left side and the value for that field on the right.
There are also ways you can export data from your production org or other sandboxes and use it here but of course, there is a data storage limit when it comes to scratch orgs. Also, it is not recommended to seed personal or sensitive data into scratch orgs.
Setting Up Scratch Orgs
Pre-requisites
The Dev Hub is a Salesforce org that you enable as the central point for creating and managing scratch orgs. It can be your production org, a Developer Edition org, or a trial Hub org. The Dev Hub org edition determines how many scratch orgs you can create daily, and how many can be active at a given point.
Commands in Salesforce DX and CLI tools
Step 1: Log into your DevHub environment:
sf org login web -d -a DevHub
Step 2: Create a scratch org by providing the path to your configuration files.
sf org create scratch -d -f config/project-scratch-def.json -a dreamhouse-org
-d: set this scratch org as the default org
-a: give this org an alias name such as dreamhouse-org
Step 3: Open your new scratch org.
sf org open
And that's it! You can now see your new scratch org which took less than a minute to create.
Additionally, you can deploy data and metadata into this new, empty org with the following commands:
//Deploy source metadata to Scratch Org
sf project deploy start
//Assign a Permission Set
sf org assign permset -n Dreamhouse
//Import data into the scratch org (give it the path to the data files)
sf data import tree -p data/sample-data-plan.json
You can delete a scratch org by using the following command or going into your DevHub environment and searching up Active Scratch Orgs in the App Launcher to manage it there.
sf org delete scratch -u dreamhouse-org -p
Practical Use Cases for Scratch Orgs
Software Engineer II at S&P Global
4 个月Great Work Myra !
Full Stack Developer | Co-Founder at PookiDevs Technologies LLC | Team Lead | Salesforce | AI & Data Enthusiast
4 个月Thank you Myra Rafique Khan - your session was quite insightful! Definitely a great help to those who are just starting with Salesforce. ??
Senior Manager/Site Lead (Salesforce Developers Pakistan) at S&P Global | Microsoft Certified .Net Developer
4 个月Great Work Myra Rafique Khan ??