Sub topic 8
Writing Apex with code completion, navigation, and diagnostics
This chapter covers what the Apex language server does while you edit. It explains which features run locally and which need an org. It also covers what to do when they stop working.
I assume you finished the earlier chapters. You have a Salesforce DX project open in VS Code, the Salesforce Extension Pack installed, a working Java JDK, and a default org authorized. You also need GreetingService from this tutorial in force-app/main/default/classes.
The working model
The Apex extension (salesforcedx-vscode-apex) does not parse Apex itself. It starts a separate Java process, the Apex language server, and talks to it over the Language Server Protocol (LSP). LSP is the standard way VS Code asks a background program questions such as "what can be typed here?" or "where is this method defined?"
The server indexes every .cls and .trigger file in your project when it starts. It answers editor requests from that index. It never sends your code to the org for these features.
Two consequences follow from this design:
- The features work offline once the index exists. You can write Apex on a plane.
- The index only knows what is on disk. Standard and custom objects exist in your org, not in your project, unless you retrieve or refresh their definitions. This is the cause of most "completion doesn't know my field" complaints.
Watching the server start
Open any .cls file. The status bar at the bottom of the window shows an Apex indicator. While the server starts and indexes, it shows a spinner. When it finishes, it shows a ready state. Features are unreliable until that happens, and large projects can take a minute or more.
If the indicator reports an error, the server did not start. The usual cause is that Java cannot be found. Check the Output panel, choose Apex Language Server in the dropdown, and read the first error. The troubleshooting chapter covers the salesforcedx-vscode-apex.java.home setting in detail.
Code completion
Completion in Apex works in three layers.
- Language keywords and system types.
String,List,Map,Database,System, and similar names complete with no setup. - Your own classes. Type
GreetandGreetingServiceappears. TypeGreetingService.and the public static methods appear with their signatures. - SObjects and fields.
Account,Contact, and custom objects complete only after their definitions are available locally.
Create a second class to try the layers. Save this as AccountGreeter.cls with its -meta.xml file (VS Code creates the metadata file when you use SFDX: Create Apex Class):
public with sharing class AccountGreeter {
public static List<String> greetAccounts(List<Account> accounts) {
List<String> greetings = new List<String>();
for (Account acc : accounts) {
greetings.add(GreetingService.greet(acc.Name));
}
return greetings;
}
}Type acc. inside the loop. If you see Name, Industry, Id, and the other Account fields, the SObject definitions are loaded. If you see nothing useful, run SFDX: Refresh SObject Definitions from the Command Palette. The command asks which objects to refresh (all, custom only, or standard only) and writes them under .sfdx/tools/sobjects. It needs a default org, so it fails if authorization is missing.
Trigger completion manually with Ctrl+Space (Cmd+Space on macOS may conflict with Spotlight, so rebind it if needed). Pressing Tab or Enter accepts a suggestion.
Snippets
The extension also contributes Apex snippets. Typing a short prefix such as for, if, or trigger and accepting the suggestion inserts a code template with tab stops. Snippets appear in the same list as semantic suggestions, marked with a different icon.
Navigation
Navigation answers "where is this thing, and who uses it?" All of these use the symbol index, not text search.
| Task | Windows and Linux | macOS |
|---|---|---|
| Go to definition | `F12` | `F12` |
| Peek definition inline | `Alt+F12` | `Option+F12` |
| Find all references | `Shift+F12` | `Shift+F12` |
| Go to symbol in file | `Ctrl+Shift+O` | `Cmd+Shift+O` |
| Go to symbol in workspace | `Ctrl+T` | `Cmd+T` |
| Show hover information | Mouse hover or `Ctrl+K Ctrl+I` | Mouse hover or `Cmd+K Cmd+I` |
| Open Problems panel | `Ctrl+Shift+M` | `Cmd+Shift+M` |
Try them in order. In AccountGreeter, put the cursor on greet and press F12. VS Code opens GreetingService.cls at the method. Now press Shift+F12 there to see every caller, including AccountGreeter and the test class.
The Outline section of the Explorer sidebar lists the classes, methods, and properties of the current file. Use it to jump around long classes.
Symbol search is different from Ctrl+Shift+F (text search). Searching the workspace for greet as a symbol returns method declarations. A text search also returns comments, strings, and metadata files.
Diagnostics
Diagnostics are the red and yellow squiggles, plus the entries in the Problems panel. The language server produces them as you type, without saving or deploying.
Remove the semicolon from the greetings.add line:
greetings.add(GreetingService.greet(acc.Name))A red squiggle appears and the Problems panel lists a parse error such as an unexpected token. This is a syntax error. The server catches syntax errors reliably because it only needs the parser.
Now restore the semicolon and call a method that does not exist:
greetings.add(GreetingService.shout(acc.Name));This is a semantic error. The code parses fine, but shout is not defined. Whether the editor flags it depends on your extension version and settings. Recent versions of the Apex extension can report semantic problems, such as unknown methods and type mismatches. Some releases gate this behind an opt-in setting, so check the extension's settings for a semantic errors option if you see nothing.
What the editor cannot tell you
A clean Problems panel does not mean the code will deploy. The platform compiler on the org is the final authority, and it knows things the local index may not:
- Fields and objects that exist in the org but are missing from your local SObject definitions.
- Namespaced and managed package types that are not in your project.
- API version differences. A feature available in version 62.0 fails in a project with an older
sourceApiVersion. - Governor limits and runtime errors, which only appear when code executes.
The reverse also happens. The editor may flag a valid reference because the index is stale. When the editor and the org disagree, trust the org's deploy result, then fix the cause: refresh definitions or restart the server.
Reading a failed deployment
When a deploy fails, the output lists the file, line, and column of each compile error, and the Problems panel is not updated for these. Fix the file and deploy again. The error text comes from the platform, so it matches what the org will say regardless of editor state.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| No completion, no hover, status bar shows an Apex error | Java not found, or an unsupported Java version | Install a supported JDK and set `salesforcedx-vscode-apex.java.home` |
| Completion works for your classes but not `Account` fields | SObject definitions missing | Run **SFDX: Refresh SObject Definitions** with a default org set |
| Go to definition does nothing | Server still indexing, or file is outside a package directory | Wait for the status bar to finish; confirm the file is under `force-app` or another path in `sfdx-project.json` |
| Squiggles on code that deploys fine | Stale index or missing definitions | Refresh SObject definitions, then restart the server or reload the window |
| New class not found by completion | File was created outside VS Code while the server was starting | Save the file, or reload the window to force a re-index |
To restart the server without closing VS Code, open the Command Palette and run Developer: Reload Window. This restarts every extension host process, including the Java server, and is the safest reset.
A short practice loop
- Open
AccountGreeter.clsand wait for the status bar to show the server is ready. - Type
GreetingService.and confirmgreetcompletes with itsStringparameter. - Press
F12ongreet, thenShift+F12to list its callers. - Introduce a missing semicolon, check the Problems panel, then fix it.
- Run SFDX: Refresh SObject Definitions, then confirm
acc.suggestsIndustry.
Exercise
You type acc. on an Account variable and get no field suggestions, although completion for GreetingService. works. What is the most likely cause, and what do you do?
Show answer
The language server has no local definition of the Account SObject, because SObject definitions come from the org and are not in your source files. Make sure a default org is authorized, then run SFDX: Refresh SObject Definitions from the Command Palette. This writes definitions to .sfdx/tools/sobjects, and completion for Account fields starts working once the server picks them up.
Exercise
The Problems panel is empty, but sf project deploy start reports a compile error in the same file. Why can this happen, and which result do you trust?
Show answer
The editor checks the code against a local index, which can be missing org-only objects, fields, or packaged types, and which may not apply every platform rule. The org's compiler is the authority, so trust the deploy result. Fix the code, and if the error came from missing local definitions, refresh them so the editor agrees next time.