Apex for Visual Studio Code
View outlines of Apex classes and triggers, see code-completion suggestions, and find syntactic errors in your code. This extension is powered by the Apex Language Server.
For best results, do not install this extension directly. Install the complete Salesforce Extension Pack instead.
Before you set up this extension, make sure that you have these essentials.
- Java 8 Platform, Standard Edition Development Kit
Some features in Salesforce Extensions for VS Code depend upon the Java 8 Platform, Standard Edition Development Kit (JDK).
If you don’t already have the JDK installed, install the latest version of the Java 8 JDK from Java SE Development Kit 8 Downloads.
- Visual Studio Code v1.26 or later
- Salesforce Extensions for VS Code
Code smartness for sObjects in Apex code is powered by the salesforcedx-vscode-core extension. We suggest that you install all extensions in the salesforcedx-vscode extension pack.
View Code-Completion Suggestions
To see code-completion suggestions, press Ctrl+space when you’re working in a
.trigger file. To navigate between the suggestions, use the arrow keys. To auto-complete a suggestion from the list, press Enter.
Insert Code Snippets
To see available Apex code snippets when you’re working in a
.trigger file, run Insert Snippet. Snippets are available for class and interface definitions, a variety of statements, and much more. These code snippets are also available as code-completion suggestions.
View or Jump to Definitions
You can preview, view, or go to definitions of:
- User-defined Apex
- Classes (from definitions of extending classes)
- Interfaces (from definitions of implementing classes)
- Variables (local and class variables)
- Standard objects
- Fields (standard and custom fields)
- Object definitions
- Custom objects
- Object definitions
(See the "Enable Code Smartness for SObjects" section of this README for information on working with standard and custom objects.)
To preview a definition, hold down Cmd (macOS) or Ctrl (Windows or Linux) and hover over the item whose definition you want to see.
To view a definition, right-click the item and select Peek Definition, or press Alt+F12.
Find All References
You can find all references to user-defined Apex:
- Class variables
To find references to an item, right-click the item and select Find All References, or press Shift+F12.
Check Syntax Errors in Your Code
If you leave out a
), the syntax error is marked with a red squiggly line in the editor.
View an Outline of Your Apex Class or Trigger
The Apex outline view shows the structure of the Apex class or trigger that’s open in the editor. For a list of the symbols in your file, press Cmd+Shift+O (macOS) or Ctrl+Shift+O (Windows or Linux). To jump to one of the symbols, select it in the list.
Enable Code Smartness for SObjects
To activate this extension’s code smartness features for standard and custom objects and their fields, including for custom fields on standard objects, press Cmd+Shift+P (macOS) or Ctrl+Shift+P (Windows or Linux), and then select SFDX: Refresh SObject Definitions from the command palette.
When you refresh your sObject definitions, VS Code uses your default org to generate faux Apex classes. These faux classes represent the standard and custom objects that the admin user has access to for your default scratch org, or the logged-in user of your sandbox or DE org. The classes are stored in a hidden directory on your local workstation. Don’t edit the faux classes! They are deleted and regenerated each time that you refresh your sObject definitions. To modify your sObjects, either modify the objects’
.field-meta.xml files (and then run SFDX: Push Source to Default Scratch Org or SFDX: Deploy Source to Org), or make changes declaratively in your org (and then run SFDX: Pull Source from Default Scratch Org or SFDX: Retrieve Source from Org). Your user doesn’t automatically gain access to new custom objects, so be sure to assign new permissions to the user as necessary. To assign permissions from the command line, run
sfdx force:user:permset:assign -n YourPermSetName.
After you add or edit standard or custom objects or their fields, be sure to rerun SFDX: Refresh SObject Definitions.
Monitor Apex Language Server Output
The Apex Language Server is an implementation of the Language Server Protocol 3.0 specification. The Language Server Protocol allows a tool (in this case, VS Code) to communicate with a language smartness provider (the server). VS Code uses the Apex Language Server to show outlines of Apex classes and triggers, code-completion suggestions, and syntactic errors. To see all diagnostic information from the Apex Language Server, select View > Output, then choose Apex Language Server from the dropdown menu. The diagnostic information gives you insights into the progress of the language server and shows the problems encountered.
Salesforce Extensions for VS Code functions properly only if the root directory of your open project contains an
If you’re not seeing the Apex completion suggestions that you expect, your Apex database might need to be rebuilt. Quit VS Code, and then delete the
.sfdx/tools/apex.db file from your project. Then relaunch VS Code, and open an Apex class or trigger. The Apex database rebuilds within about 5 seconds (up to 30 seconds for very large code bases).
Bugs and Feedback
- Trailhead: Get Started with Salesforce DX
- Doc: Salesforce DX Developer Guide
- Doc: Apex Developer Guide
- Dreamforce ’17 session video: Building Powerful Tooling For All IDEs Through Language Servers
- GitHub: Language Server Protocol
- GitHub: salesforcedx-vscode-apex
- GitHub wiki for salesforcedx-vscode: Apex Language Server
Currently, Visual Studio Code extensions are not signed or verified on the Microsoft Visual Studio Code Marketplace. Salesforce provides the Secure Hash Algorithm (SHA) of each extension that we publish. Consult Manually Verify the salesforcedx-vscode Extensions’ Authenticity to learn how to verify the extensions.