Rob Cowell · 30th September 2024
Why use Salesforce scratch orgs?
Salesforce has long been more than just a CRM tool — it’s a full-blown development platform. As such, teams need better tools to manage customizations and move them through development pipelines. One approach is to make use of scratch orgs. In this post, we’ll dive into what scratch orgs are, why they’re a useful technique in modern Salesforce DevOps and CI/CD practices, how to set them up, and compare them to traditional sandboxes.
What are scratch orgs?
A scratch org is a temporary, disposable Salesforce environment which were introduced with Salesforce DX. Unlike permanent Salesforce environments like sandboxes, scratch orgs are designed to be lightweight, customizable, and short-lived — perfect for development and testing in rapid iteration cycles. They can be spun up quickly, populated with specific metadata or configurations, and torn down once they’ve served their purpose.
Scratch orgs are fully configurable to emulate different Salesforce environments (like Professional or Enterprise editions) and can be shared or set up as personal orgs for developers. Because of their configurability and transient nature, they have become central to modern Salesforce development, especially in DevOps pipelines.
How scratch orgs fit into Salesforce DevOps and CI/CD
Scratch orgs bring many benefits to your Salesforce DevOps implementation:
Short-lived and easily reproducible
Scratch orgs can be created on demand, ensuring that each developer works in an identical, isolated environment. This eliminates the "works on my machine" problem and improves collaboration. Most teams using scratch orgs tend to create them for each new feature they work on, but you could also create daily scratch orgs if that better fits your process.
Automated testing
One of the primary benefits of scratch orgs is their ability to be integrated into CI/CD pipelines for automated testing. Since they can be set up with a predefined set of configurations, they are ideal for running unit tests, Apex tests, and even UI tests without affecting production environments.
Version control integration
In a source-driven development model, version control (like Git) becomes the source of truth. Scratch orgs pull their configurations and metadata from version control, allowing teams to rapidly develop and test new features in a controlled, trackable way. You can easily check out a branch, create a scratch org for your development project, test the changes, and merge it back once approved.
Continuous delivery
Scratch orgs allow for seamless integration into CI/CD pipelines by enabling automated deployments. Whether you’re using third-party tools for your deployment process, or Salesforce's own DevOps Center, scratch orgs streamline the testing and promotion process from development to production.
Reduce downtime and error risk
Scratch orgs help identify and resolve errors early in the development process, reducing the risk of bugs reaching production. They also ensure that developers aren’t competing for shared resources, like in a shared sandbox, which can lead to conflicts and errors.
The value of using scratch orgs for testing
The ability to spin up isolated environments that mirror production for testing is invaluable. With scratch orgs, you can:
- Run automated tests: Every commit to version control can trigger the creation of a scratch org, run a suite of automated tests, and provide feedback on whether the changes are working correctly.
- Test new features in parallel: Developers can test different features or fixes in parallel by creating multiple scratch orgs. Each org provides a safe space for experimentation, without affecting the work of others or the primary production environment.
- Mock different Salesforce editions: If you’re developing a managed package for the AppExchange, you can use scratch orgs to test it across multiple Salesforce editions and configurations.
How to set up scratch orgs
Setting up a scratch org requires Salesforce DX and the Salesforce CLI. Here’s a basic walkthrough of how to create a scratch org:
1. Install the Salesforce CLI
This is the core tool you’ll need to interact with Salesforce DX and scratch orgs.
2. Enable Dev Hub
A Dev Hub org is required to create and manage scratch orgs. It serves as the control center for all your scratch orgs and is typically created from your Production org.
3. Define your org shape
You can use a JSON file (project-scratch-def.json
) to define the features and settings your scratch org will have. This file outlines your desired configurations like the Salesforce edition, enabled features (e.g., Multi-Currency, Lightning), and object permissions. The aim is to replicate a Salesforce org with the features and configuration that your development and test work depends upon.
4. Create a scratch org
Using the Salesforce CLI, run the command sf org create scratch --definition-file config/project-scratch-def.json --alias MyScratchOrg --duration-days 7
. This will create a scratch org that will last for 7 days and be aliased as MyScratchOrg.
5. Push metadata
Push your source code to the scratch org using sf project deploy start
, which will move all metadata and configurations from your version control system into the new org.
6. Use it for development and testing
Once the org is ready, you can use it for whatever purpose you need — whether that’s feature development, testing, or bug fixing.
7. Delete it
Once you’ve finished, you can tear it down with the command sf org delete scratch --target-org MyScratchOrg
. This will remove it from the active scratch orgs associated with your Developer hub.
Scratch orgs vs sandboxes: key differences
While sandboxes have long been a staple in Salesforce development, scratch orgs bring a few advantages that make them better suited for certain use cases:
Feature | Scratch orgs | Sandboxes |
---|---|---|
Lifespan | Temporary (days or weeks) | Persistent (months or years) |
Creation time | Minutes | Can take hours |
Customization | Highly configurable with JSON definitions | Pre-defined types (e.g., Developer, Full) |
Use case | Development, testing, CI/CD | Long-term environments (e.g., UAT, Staging) |
Integration | Seamless with CI/CD and version control | Traditionally more manual |
Data | No data included by default, must be seeded | Partial or full copies of production data |
Scratch org limitations
While they deliver significant benefits for the development lifecycle over sandboxes, scratch orgs do come with some limitations that you will need to consider.
Scratch org allocation
Depending on the Salesforce edition of your Dev Hub org, different limits apply to how many scratch orgs you can have active at any given time and how many you can create per day.
- Developer Edition: 3 active scratch orgs, 6 daily scratch org creations
- Enterprise Edition: 40 active scratch orgs, 80 daily scratch org creations
- Unlimited and Performance Edition: 100 active scratch orgs, 200 daily scratch org creations
Scratch org users
The number of users you can create in a scratch org is determined by the user licenses provided by the scratch org edition and is influenced by the licenses in your production org. For instance, a Developer Edition scratch org usually includes two Salesforce user licenses, allowing for one additional standard user besides the default administrator. In other editions, the available licenses in your production org dictate the number of users you can create in a scratch org, mirroring the license limits of your primary environment. This approach ensures that scratch orgs realistically reflect the constraints of your production setup for better testing and development alignment.
Scratch org expiry
When a Salesforce scratch org expires, it becomes inaccessible, and all data, metadata, and customizations within it are permanently deleted. Salesforce automatically handles cleanup, removing the expired org from your list.
To avoid losing progress, developers should regularly push work to version control and track expiration dates using the Salesforce CLI, ensuring they can quickly spin up new scratch orgs without disruption to development or CI/CD pipelines.
Ready to start using scratch orgs?
Salesforce scratch orgs have become a cornerstone of modern DevOps practices in the Salesforce ecosystem. Their lightweight, flexible, and temporary nature makes them perfect for rapid development and testing, especially in continuous delivery pipelines. When combined with version control and automation tools, scratch orgs empower teams to build faster, test more thoroughly, and deploy more reliably, minimizing risks and boosting productivity.
If you haven’t started using scratch orgs yet, why not explore how they can enhance your Salesforce DevOps strategy, alongside the other benefits of Salesforce DX? Whether you're a developer, admin, or architect, or just curious, scratch orgs can bring flexibility and agility to your workflows.