Visual Studio Code and Salesforce DX

Sep 13, 2017

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

Important Notes Before Getting Started

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

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.

Create a Dev Hub Org with Salesforce DX Enabled

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.

Install the Salesforce CLI and Configure DX

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:

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 Allow when 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.

Clone a SFDX Sample Project

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:

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.

Using the VS Code Extension for SFDX

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.

Signup to get blog posts about Salesforce development, source control and Continuous Integration every week.