salesforce dx scratch orgs

One of the coolest concepts to come out of Salesforce DX is the Scratch Org. According to Salesforce:

Salesforce DX introduces a new type of Salesforce environment, the scratch org, a source-driven and disposable deployment of Salesforce code and metadata. Scratch orgs are fully configurable, allowing developers to emulate different editions with different features and preferences, playing a critical role in driving developer productivity and collaboration during the development process. They can also be used as part of automated testing and the implementation of a full continuous integration suite.

Setting up appropriate development environments for Salesforce can be a major challenge. Full copy sandboxes can take days to provision and even simpler partial copy sandboxes can take a long time to set up. Worse, there’s no way to spin up a sandbox environment programmatically which is key to any Continuous Integration setup. Even if you could programmatically spin up a development environment or sandbox in Salesforce, how do you load data onto that environment? Suffice to say, Salesforce has historically made it difficult to set up clean environments for testing and development.

Meanwhile, a revolution has been happening in software development and it’s called Docker. With Docker, you can quickly spin up small virtual containers (as opposed to slow and cumbersome full virtual machines) that are perfect for CI. Of course, you can’t use Docker with Salesforce in this way, but the promise of Scratch Orgs is that you could do something very similar to Docker for the Salesforce environment.

So What Are Scratch Orgs?

Scratch Orgs are designed to make source driven development and CI on the Salesforce platform more similar to the type of CI experience you might have building with Node, Rails or Django using Docker. They are meant to be temporary rather than long lasting. Spin them up quickly and throw them away just as quickly. Get into a bad state on your Org? Throw it away and create a new one. Salesforce is talking about imposing some kind of limits on how long you can keep a Scratch Org though I don’t think they’ve decided yet. We’ve heard anywhere from a day to two weeks.

A key tenet of Scratch Orgs is that you can spin them up quickly. Blue Canvas is fortunate enough to be a part of the Salesforce DX developer preview. In our experience so far, we have consistently been able to spin up Scratch Orgs quickly. This is a major improvement over how long it takes to spin up other Orgs on Salesforce.

Scratch Orgs can be created via the Command Line Interface (CLI). This is nice because it makes it possible to easily integrate them into a CI pipeline using Jenkins or Bamboo.

Salesforce DX is also promising to make it easier to upload data to your Scratch Orgs. Now you won’t be able to upload all of your application data to a Scratch Org, but you can create scripts to automatically populate data so that you don’t have to manually do this every time you spin up a new environment. This is another key step in the direction of enabling CI.

Our Hopes for Scratch Org Use

How do we envision leveraging Scratch Orgs? Let’s say you have a feature that you want to build. First you open up a feature branch in Git. Next you can use the Salesforce CLI (an update for which is coming with DX apparently) to spin up a Scratch Org in just a minute or so. You start coding your new feature and testing it manually against the temporary Org. Add a few Apex Classes, a Visualforce page or two and voila: you’re happy with it.

Next, you commit that new code into your feature branch and merge it to master. Typically your CI server (whether it’s Jenkins, Bamboo, TeamCity or whatever) will kick off a build. You would have already written a build step into your CI job that would use the CLI to spin up another Scratch Org. Once this is created you can use an Ant script to deploy your new code to that Org. You can use the data loading feature in Salesforce DX to populate any test data that you need.

Now you can run your tests. If they pass, you can use your Ant script to deploy your changes to your UAT environment. Or something like that.

A lot of unanswered questions remain about Scratch Orgs and Salesforce DX, but they are very promising. As Salesforce continues to unveil new information we’ll update this post. Scratch Orgs are definitely a step towards making Salesforce a first class development environment. We look forward to playing with it next summer!