Sub topic 9
Deploying and retrieving source
This chapter assumes you have a Salesforce DX project, an authorized default org, and the Salesforce Extension Pack working in VS Code. If any of those are missing, finish the earlier chapters first.
How source moves between project and org
Your local project is the source of truth. The org holds a copy. Two operations keep them in sync:
- Deploy sends local files to the org.
- Retrieve pulls the org's version into your local files.
Both use the default org you set when you authorized it. The Apex language server does not take part in this step, but it needs a valid project so that the editor knows which files to treat as Apex.
Deploying from the command line
Start with the class from earlier chapters, GreetingService, and its test class. Deploy everything under force-app with the CLI:
sf project deploy start --source-dir force-appThe command prints a table of the components it deployed and whether each one was created or changed. If a component fails, the output names the file, the line, and the error message. Fix the file and run the command again.
To deploy a single file, point --source-dir at it:
sf project deploy start --source-dir force-app/main/default/classes/GreetingService.clsDeploying from the editor
The extension pack wraps the same commands. In the Explorer, right-click a file or folder and choose one of these:
- SFDX: Deploy This Source to Org sends the selected file or folder.
- SFDX: Retrieve Source from Org pulls the org's version of the selected item.
You can also open the Command Palette and run SFDX: Deploy Source to Org while a file is open in the editor. The Output panel shows the same result table you would see in the terminal.
Retrieving source
Retrieve when someone changed the org directly, for example with a setup page or a teammate's deployment. Use the CLI:
sf project retrieve start --source-dir force-appA retrieve overwrites local files with the org's version. Commit or stash your work first so that you can compare the result and recover if needed.
Preview before you act
Deploy and retrieve can conflict if the same component changed on both sides. Preview the changes first:
sf project deploy preview
sf project retrieve previewThe preview lists what would be deployed, retrieved, or deleted, and flags conflicts. It does not change anything.
Source tracking applies only to scratch orgs and some sandboxes. With a Developer Edition org or a production org, there is no tracking. In that case you name the files to deploy or retrieve, as shown above.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No default org found | No org is authorized or the default is not set | Run `sf org login web --alias my-org --set-default`, then `sf org list` |
| Expired access or refresh token | The authorization has lapsed | Log in again with the same alias |
| Component failure on deploy | A compile error, or a missing dependency in the org | Read the file and line in the output, then fix and redeploy |
| Nothing deployed | `--source-dir` points at the wrong folder | Check the path against `packageDirectories` in `sfdx-project.json` |
Verify your work
Deploy the project and check that it succeeded:
sf project deploy start --source-dir force-appThen open the org in a browser, go to Setup, search for Apex Classes, and confirm that GreetingService appears.
Exercise
You changed GreetingService.cls locally, and a teammate edited the same class in the org. Which command lets you see the conflict before you overwrite either version, and why should you commit your work before running a retrieve?
Show answer
Run sf project deploy preview or sf project retrieve preview. They list the components that would change and flag conflicts without changing anything. Commit first because a retrieve overwrites local files with the org's version, and a commit lets you compare and recover your edits.