Developer Tips

MavensMate for Visual Studio Code

Turn your text editor into the Salesforce Developer Console with MavensMate for Visual Studio Code.

Last Update:
Published:
April 11, 2017

Table of Contents

MavensMate is one of the coolest open source tools in the Salesforce ecosystem. It has become so popular that many people assume that Mavens Consulting - the company which employs creator Joe Ferraro - is in the business of making IDE plugins for Salesforce (it’s actually a major consultancy for healthcare applications written on the Salesforce platform among other things). Because of the generosity of Joe and Mavens, Salesforce developers around the world have a unique tool which allows you to more easily write code for Salesforce.

What does MavensMate do? It does a number of things, but it essentially grants many of the major desktop text editors like Visual Studio Code, Sublime Text, and Atom a lot of the powers directly embedded in the Salesforce Developer console. For example, in the developer console, I can click New Apex Class and be given a template for an Apex Class. When I fill it out with some actual code and click save, that class is now live in my org. MavensMate allows you to do the same. You can connect specific Salesforce orgs or sandboxes to MavensMate. When you update code in a text editor with the plugin installed, it automatically deploys that change to the org you connected upon save. Many developers make this a daily part of their routine for getting code into their sandbox for testing.

The best part about MavensMate is that it’s open source and free. You can download MavensMate online and checkout GitHub for MavensMate documentation. Given that it’s open source you can even contribute your own features back to the project through the project’s GitHub page. MavensMate offers plugins for Visual Studio Code, Sublime Text and Atom.

MAVENSMATE OVERVIEW - DESKTOP CLIENT

MavensMate essentially comes in two parts: the desktop client and the editor plugins. The MavensMate desktop client is agnostic to which editor plugin you use, and provides set up and configuration options for your IDE, as well as some other cool features.

Deploy to Orgs with MavensMate

deployment-connection-min.png

The most popular feature of MavensMate is setting up your IDE to deploy directly to your Salesforce sandbox. You can use MavensMate to connect your sandbox to your code editor. MavensMate employs Oauth 2.0 to do this. Setting up a connection in this way allows you to deploy code directly to your org from your text editor. When you save, MavensMate will validate that your syntax is okay before deploying. Later in this post we’ll show you how to use the Visual Studio Code plugin to make this happen automatically.

Create New Apex Classes and Triggers

Creating Apex Classes and Triggers isn’t always straightforward in Salesforce because each file requires an associated XML file to accompany it. Much like using the developer console to create these, the MavensMate’s desktop client has a tool for automatically generating the xml files for new Apex Classes. It comes replete with templates that you can choose from to simplify the formatting of your class. For example, this BDD Unit Test Template for Salesforce:

Or a Selector:

Creating New Lightning Components with MavensMate

create-lightning-component-min.png

MavensMate also has templates for creating Lightning Components, Apps, Events and Interfaces that let you interact in the same way. As we all make the transition to Lightning, this can be very useful.

Run Apex Unit Tests, Execute Apex, and Debug Logs

Automated testing is an important part of any CI workflow. MavensMate streamlines and unifies the unit testing experience for Salesforce. Not only does MavensMate provide Apex unit test templates, it also provides a test runner that allows you to kick off and run your tests. It also provides a nice graphical user interface for seeing how much of your code is covered by your tests. This is useful for knowing how to spend your time when you write tests. It also makes debugging much less painful than using the Salesforce developer console.

MAVENSMATE EDITOR PLUGINS

The other useful side of MavensMate are the editor plugins for Sublime Text, Visual Studio Code and Atom. They basically turn your text editor into the Salesforce developer console. So you get all the benefits of your text editor’s syntax highlighting while still being able to quickly save your changes to your sandbox and test them out there.

MavensMate Setup

Regardless of which editor plugin you want to use, the first step is to install the MavensMate desktop client. MavensMate Desktop client is available for download on GithHub.

MAVENSMATE AND VISUAL STUDIO CODE

This tutorial assumes you already have Visual Studio Code installed. So the first step is installing the MavensMate plugin for VS Code. Go to the extensions panel in VS Code, search for MavensMate, and install the plugin.

install-vs-code-extension-min.png

To test that it’s installed open the command palette with ctr+shift+P (Windows) or cmd+shift+P (OSX) and type “mavensmate” in the command prompt. You should see a list of autofilled suggestions like so:

command-prompt-min.png

Next, you will want to connect your sandbox to MavensMate. Go to the desktop client and select New Project. Select your org type from the dropdown (e.g. Production vs. Sandbox vs. Developer):

From here you’ll be prompted with an Oauth flow to connect to your Salesforce Org. When you do this connection, MavensMate will download all of your code and configuration metadata for that org. You’ll be prompted for access to your Org. To move forward you must click Allow.

Name your project something that you will remember.

Next you will need to tell MavensMate which metadata to track. MavensMate will only track metadata which you tell it to. To enable metadata visit the Advanced tab in MavensMate. Use the selection tool to tell MavensMate which types to track.

advanced-min.png

Next, go to the Metadata tab and select all the types that you want to be tracked. Once that is all set, click Create Project.

Note: You can always update the metadata types you track by going to Edit Project.

Now if you click Open in Vscode your project will open up in Visual Studio Code. If you make changes and save them, they should show up in your org. Status messages will show up at the bottom of the screen. Depending on the size of your org and the number of types tracked, you may find that it takes up to a few minutes to sync.

Now you’re ready to code in Visual Studio Code and have it connect directly to your Salesforce sandbox.

INTEGRATING BLUE CANVAS WITH MAVENSMATE

MavensMate and Blue Canvas compliment each other well. MavensMate turns your text editor into something approximating the Salesforce Developer Console. Blue Canvas can automatically pick up changes made in either the Developer Console, or by MavensMate and commit it into source control. This is useful to prevent developers from overwriting each other’s code and allowing you to have a clear audit history of changes made in your Salesforce development pipeline. Blue Canvas also allows you to migrate code simply between orgs as a part of continuous integration for Salesforce.

Connecting your orgs to Blue Canvas takes four clicks. To learn how to do this see the Blue Canvas quick start documentation. Once connected, you will see all of your changes automatically show up in Git and have the ability to migrate code from one org into another.

More like this