How to install and use the Visual Studio Code Extension Pack for Salesforce DX.
As we mentioned in our last post, MavensMate will no longer be supported going forward. In his farewell message, MavensMate creator Joe Ferarro suggested users check out the Visual Studio Code Extension Pack for Salesforce DX. At the time of writing there are over 5,000 installs of the extension but no reviews so we thought we’d do a little write up.
The extension is still in Preview mode and is not officially GA. That said it works fairly well and you can use it. It’s also open source so you can submit issues and pull requests! It’s pretty quick - the whole process took me about 20 minutes to install and get up and running and it’s a great introduction to Salesforce DX if you haven’t already started to use it.
(A quick word on VS code: it’s awesome, lightweight and we use it.)
As far as we can tell, the Visual Studio Code Extension Pack for Salesforce DX is not a replacement for MavensMate for Visual Studio Code. First and foremost, it does not work with Sandboxes so it will not work in your typical developer workflow. It works only with Scratch Orgs at the moment.
The extension is actually four mini-extensions:
The first is basically a DX/CLI wrapper for Visual Studio Code. It ports many of the things you can do from the command-line with the Salesforce CLI into the Command Palette of Visual Studio Code. This is nice if you for some reason don’t like to go to the command-line. The rest basically provide code completion and syntax highlighting for various Salesforce types like Apex Classes and Visualforce Pages.
IMPORTANT: In order to use the Visual Studio Code with Salesforce DX extensions, you need to set up the Salesforce CLI and start a Salesforce DX project.
You can read more about the plugins in the Visual Studio Code marketplace. Ultimately you will install the plugin bundle here, but while the tool is still in Preview they suggest manually verifying the salesforcedx-vscode extension’s authenticity. This is to make sure you are downloading a secure version of the extension. The link above will give you instructions for how to do this.
Download the extension manually here. From a terminal run:
shasum -a 256 <location_of_the_downloaded_file
Verify that the SHA is in the list of SHAs provided in the file provide.
Change the extension of the file you downloaded from .zip to .vsix.
In Visual Studio Code, go to Extensions, click to options and select Install from VSIX:
You may need to reload VS Code to make the installation go through.
You need to enable DX to use the extension. You can do this in your production or business org. If you just want to test it out quickly, do what I did and create a Dev Hub here. Signing up will give you an environment where you can create multiple scratch orgs for testing.
There is a good Trailhead module on setting up Salesforce DX that can help with the basics.
Next you need to set up the Salesforce CLI. You can download it for Windows, Mac and Ubuntu here. Note: you may be able to set this up directly from VS Code, but it’s useful to have a good command of the CLI as well. To confirm that it worked type sfdx into the terminal. You should see something like this:
Usage: sfdx COMMAND [command-specific-options]
Help topics, type "sfdx help TOPIC" for more details:
sfdx force # tools for the salesforce developer
sfdx plugins # manage plugins
sfdx update # update sfdx-cli
If you see that you know it worked. Then you need to configure SFDX and create some Scratch Orgs. Authorize the CLI to work with your DevHub by typing:
sfdx force:auth:web:login -h
This will automatically open your browser and prompt you to login to the dev hub. Use the credentials that you used to create the Salesforce DX authorized dev hub. In our case, we are using a special hub that we created just for this demo. Click Allowwhen the Oauth Flow prompts you to.
Once you’ve gone through this authorization process you can set up an alias which makes it easier to remember which dev hub you are working with. Remembering an alias is much easier than remembering the username that you logged in. In this case DevHub is the creative alias we have chosen:
sfdx force:auth:web:login -d -a DevHub
You should see something like:
Successfully authorized <your-username> with org id 00B1I000000L9r3UCY
You may now close the browser
If you want to open the dev hub you can do so here:
sfdx force:org:open -u DevHub
But there is no need yet.
Salesforce DX has a few sample projects that are perfect for getting started. We downloaded sfdx-simple with Git which contains two visualforce pages, an Apex test, and a controller.
git clone https://github.com/forcedotcom/sfdx-simple.git
You can then create a scratch org:
sfdx force:org:create -s -f config/project-scratch-def.json
It may take a few minutes but it’s fairly quick. It gives you a username for the org:
Successfully created scratch org: 00D0v0000000an1EAA, username: <sample-email>
Configure that username to be default so that you are pushing to that scratch org and not some other environment:
=== Set Config
Push your code to the scratch org (note that the code that you have cloned from Git won’t be on the scratch org until you push it initially):
STATE FULL NAME TYPE PROJECT PATH
───── ───── ───── ─────
Add DemoController ApexClass force-app/main/default/classes/DemoController.cls-meta.xml
Add DemoController ApexClass force-app/main/default/classes/DemoController.cls
Add DemoPage ApexPage force-app/main/default/pages/DemoPage.page-meta.xml
Add DemoPage ApexPage force-app/main/default/pages/DemoPage.page
Add DemoControllerTests ApexClass force-app/test/default/classes/DemoControllerTests.cls-meta.xml
Add DemoControllerTests ApexClass force-app/test/default/classes/DemoControllerTests.cls
Open the app to confirm:
sfdx force:org:open --path one/one.app
Waiting to resolve the Lightning Experience-enabled custom domain..........
This takes a few minutes in my experience. It should open the scratch org in the browser eventually. When it’s ready it will also give you a URL that you can use to open it.
Open up the Visualforce page to confirm that it’s working.
Now that we have SFDX up and running with scratch orgs working we can use the VS Code extension instead of the command line.
Open the project in VS code. Navigate to the VisualForce Page and make a few changes. Save.
Pull up the command palette in VS Code by typing Cmd+Shift+P on Mac or Ctrl+Shift+P on Windows. Type sfdx and it will prompt you with some command-line-esque options. You can select Push Source to Default Scratch Org.
When you do this it will notify you when it successfully pushes to the scratch org.
If you click Show you can see the output.
Refresh your Visualforce page and see that your change has been made.
That’s it! It’s worth noting that it’s not as simple as a MavensMate save. And it only works with Scratch Orgs, not with Sandboxes. We look forward seeing more from Salesforce DX and the extension in the coming months.
Downstream deployments are crucial for a good Salesforce sandbox strategy, but sandbox refreshes are limited. Git can help with selective deployments.
Blue Canvas will attend Dreamforce in a low-key style, but they want to meet attendees to demo new features and ideas. They'll be in the Dev Zone and attending two parties.