SFDX+VSCode : Scratch Org Automation with VSCode Task
If you or your team is considering moving to SFDX (or already have moved to SFDX recently) and you are looking to save every admin and developer’s time by automating the scratch org creation, this article is for you.
Creating a scratch org itself only takes a few minutes. However, setting up the scratch org for actual development can take a while — installing managed packaged used in prod environment, assign permission sets, seeding the scratch org with dummy data, resetting password or creating test user, and pushing the source. Thankfully, those tedious manual job can be automated using SFDX commands (as it is a well known second nature of a programmer: automate anything that takes more than 2 minutes).
If you or your team wants to automate the scratch org creation process but you haven’t done the manual process of doing it yet, I suggest that you go try the process from start to finish manually. A couple times, maybe. Understanding the process yourself will allow you to actually understand the script rather than just copy/pasting the text, and will save your time long term expanding and customizing the script the way your team needs it.
Once you understand what the manual process looks like, start writing down the process. Following is what my process looks like:
- Create scratch org
- Install dependent packages
- Push source
- Assign Permission Sets
- Insert dummy test records
- Open scratch org
To keep this article focused on demonstrating VSCode task and script, I’ll only include steps for creating and opening the scratch org.
You’ll be creating a shell script that will run the SFDX commands for you, so you can save your previous 2 minutes creating and opening the scratch org.
Salesforce provided this example script that installs a package with dependencies. It gives you the idea of the shell script with SFDX command looks like.
I’ll start by creating a new shell file that will contain the commands. Create new file at the SFDX project root:
scripts\create_scratch_org.sh This will create a new folder
scripts and will create a new empty shell file. As mentioned above, I’ll add following two commands:
- SFDX commands to create scratch org:
sfdx force:org:create -f <scratch org definition file location> --setalias <some alias> --durationdays <scratch org duration> --setdefaultusername
- SFDX commands to open the created scratch org:
Following is the example script including the two main commands and some extra echo and shell script specific configurations.
You can test the script by
$ sh scripts/create_scratch_org.sh (Window users! If you are getting an error
sh:The term ‘sh' is not recognized... : try updating default terminal to be git bash) (Mac users! If you are getting an error related to permission, run
chmod +x scripts/create_scratch_org.sh to provide permission for your Mac to run the script)
The script (sort of tried to) created a scratch org, then opened a default org! It is now ready for better ways to run it other than typing
$ sh scripts/create_scratch_org.sh every time you need the script to run.
VSCode Task And How To Configure One
VSCode Task provides a way for programmers to create a ‘task’ that can be easily run via the command palette. VSCode documentation provides a great example of integrating the task with external tools.
VSCode Tasks are defined in
.vscode/tasks.json path in your SFDX project root. You can easily create the task template file by opening the command palette ( Windows:
Ctrl+Shift+P / Mac:
cmd+Shift+P ) then start typing
tasks: configure task. Select “Create tasks.json file from template” then “Others.” This will create a following file in the path
Now we’ll just need to update this file so it runs the script we just built. Let’s first change the label to be something more descriptive — “Create Scratch Org.” then for the command, make sure it runs the shell script we made.
You just created a VSCode task that saves your team the previous development minutes! All you need to do to run the script is to open the command palette ( Windows:
Ctrl+Shift+P / Mac:
cmd+Shift+P ), start typing and select
tasks: run task then select the task to create a scratch org (then select ‘Continue without scanning the task output’)
Now that the script and the VSCode task files are configured, you can add in more SFDX commands as your team needs it. If you are following the code example, I suggest maybe you try to let users choose the scratch org alias and a duration amount by adding ‘input’ variable to the
tasks.json file and update the script to take 2 args.
Happy Programming! 💻
- Visual Studio Code — Integrate with External Tools via Tasks: https://go.microsoft.com/fwlink/?LinkId=733558
- Visual Studio Code — Variables Reference-input variables: https://code.visualstudio.com/docs/editor/variables-reference#_input-variables
- Salesforce DX Developer Guide — Sample Script for Installing Packages with Dependencies: https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_dev2gp_install_pkg_sample_script.htm?search_text=dependencies
- Salesforce DX Developer Guide — Create Scratch Orgs: https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_scratch_orgs_create.htm