I really like working with Salesforce DX and scratch orgs. However, in larger projects it can be time consuming to create a scratch org, install dependencies, push code, assign permission sets and setup test data. After doing this manually for a while I created a PowerShell script to automate the process. In a large project the script can still take some time to run but at least I can let it run while I do something else.
Not everyone will need to perform all of the same tasks for scratch org creation so think of the following sections like Lego blocks you can snap together to create a complete scratch org creation script. The only one that is required is the first block. That’s the block that actually creates the scratch org.
Create Scratch Org
The first script block accepts three parameters and all of them are optional.
- orgName: The name of the scratch org.
- duration: The duration of the scratch org.
- debugMode: True or False. If True it will enable debug mode. This will prevent JavaScript from being minified and can make it easier to debug Aura and Lightning Web Components.
If you don’t provide parameters then a default name and duration will be used and debug mode will not be enabled.
Install Package Dependencies
If your project depends on packages this block will install them. You will need to modify this script so the package id comes after the -p parameter and the package password comes after the -k parameter. If the package does not have a password you can remove the -k parameter. Then repeat this block for each package you need to install.
Deploy Code and Metadata
The next block will deploy your source code and meta data to the scratch org. This block should work without any modifications.
Assign Permission Set
This block will assign permission sets. Enter the name of the permission set after the permsetname parameter and repeat that entire line for each permission set you need to assign.
Populate Test Data
I like to always have a set of known test data in my scratch orgs. Manually creating records for testing can be very time consuming but this block automates it. If you aren’t familiar with using the Salesforce CLI to import data then stay tuned. That will be the subject of my next post.
To use this block enter your plan file after the –plan parameter.
This script has saved myself and my co-workers many hours that would have been spent manually creating scratch orgs. I hope you find it useful.