Sub topic 3
Deploying and Retrieving Apex Source
Assume you already have a Salesforce DX project, an authorized default org, the Salesforce CLI, and the Salesforce Extension Pack installed. Deployment sends project source to an org. Retrieval copies metadata from an org into the project.
Deploying and Retrieving Source
Use the terminal or the Command Palette commands such as SFDX: Deploy Source to Org and SFDX: Retrieve Source from Org.
sf project deploy start --source-dir force-app --target-org my-org
sf project retrieve start --metadata ApexClass:GreetingService --target-org my-orgDeployment output reports changed components and errors. Retrieval can overwrite local files, so save or commit local changes first.
flowchart TD
A[Edit Apex in VS Code] --> B[Deploy source]
B --> C[Run Apex tests]
C --> D[Inspect results and logs]
D --> E[Replay the debug log]
E --> Acaption: The normal edit, deploy, test, and debug cycle alt: A flow from editing Apex through deployment, testing, log inspection, and replay debugging
Running Apex Tests and Reading Results
Run local tests after deployment. The human result format shows each test and its failure message.
sf apex run test --test-level RunLocalTests --result-format human --wait 10 --target-org my-orgA passing test confirms behavior for the tested case. A failed test usually includes the assertion, class, method, and line number. Open that line in VS Code, fix the code, and run the test again. The Testing view can run individual methods or complete test classes.
Running Anonymous Apex and Collecting Logs
Anonymous Apex is code that runs without saving a class. In VS Code, open the Command Palette and choose SFDX: Execute Anonymous Apex with Currently Opened File.
System.debug(GreetingService.greet('Ada'));Select the target org when prompted. The execution creates a debug log if logging is enabled. Open it from the Output panel or the Salesforce log commands.
Debugging with the Apex Replay Debugger
The Apex Replay Debugger reads a captured log and replays the execution locally. It does not pause the live transaction. Set checkpoints or suitable log levels, execute the code again, then choose Launch Apex Replay Debugger with Current File. Use the editor controls to step through lines and inspect variables.
Common Setup Problems
| Symptom | Likely cause | First check |
|---|---|---|
| Apex language features do not start | Missing or incompatible JDK | Check Java and the Apex extension output |
| Deployment says no default org exists | The project has no target org | Run `sf org list` and authorize an org |
| Retrieved files appear in the wrong place | Incorrect project structure | Check `sfdx-project.json` and package directories |
If the extension cannot find Java, set its JDK path in VS Code settings:
{
"salesforcedx-vscode-apex.java.home": "/path/to/your/jdk"
}Exercise
Which operation should you use to copy an Apex class from an org into your local project?
Show answer
Use sf project retrieve start, or the VS Code command SFDX: Retrieve Source from Org.