SFDX+VSCode : Scratch Org Automation with VSCode Task

From starecat.com

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).

Automate What?

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.

Script Automation

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: sfdx force:org:open

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)

Obviously I’ve created one too many scratch orgs testing out for the article, but the script (that did not ‘set -e’) continued running and opened the default scratch org that was created previously.

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 .vscode/tasks.json

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’)

Customize Script

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! 💻

Resources

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store