Sub topic 8
Deploy and retrieve metadata
This chapter assumes you have installed the sf CLI, authorized an org, and set a default target org. It also assumes you have generated a Salesforce DX project and are working inside its folder. If any of that is missing, finish the earlier chapters first.
The working model
A Salesforce DX project keeps metadata as source files on your machine. Metadata is the configuration and code of an org, such as Apex classes, custom objects, and page layouts. Two commands move it between your project and an org:
sf project deploy startsends local source files to the org.sf project retrieve startpulls the org's versions of metadata into your project.
Both commands act on the default target org unless you name another one. Both also read sfdx-project.json to find your package directories, such as force-app.
Deploy source to an org
Run this from the project root:
sf project deploy start --source-dir force-app
sf project retrieve start --source-dir force-appThe first command deploys everything under force-app to the default target org. The CLI prints a table of the components it deployed, with a state such as Created, Changed, or Unchanged. If a component fails, the output lists the file, the line, and the error message.
The second command retrieves the org's versions of the same metadata back into force-app. Retrieving overwrites local files that differ from the org, so commit or back up local edits first.
Choose what to move
--source-dir is one way to select metadata. You can also select by metadata type or by a specific component:
| Flag | Short form | What it selects |
|---|---|---|
| `--source-dir` | `-d` | A folder or file path in your project |
| `--metadata` | `-m` | A metadata type or a named component, such as `ApexClass` or `ApexClass:MyClass` |
| `--manifest` | `-x` | The components listed in a `package.xml` file |
For example, deploy a single Apex class like this:
sf project deploy start --metadata ApexClass:MyClassYou can repeat a flag to select more than one thing. Narrow deployments are faster and make failures easier to read.
Target a specific org
Use --target-org (short form -o) to override the default for one command. Pass an alias or a username:
sf project deploy start --source-dir force-app --target-org myorgNaming the org explicitly is a good habit in scripts. It prevents a deployment from landing in the wrong org if someone changes the default.
Preview before you deploy
Run a preview to see what would change without changing anything:
sf project deploy preview --source-dir force-appThe preview lists the components that would deploy, the ones that would be retrieved, and any conflicts. A conflict means the same component changed in both your project and the org.
To check a deployment without saving it, use a validation run:
sf project deploy validate --source-dir force-appValidation runs the same checks as a deployment, including tests when the org requires them, then rolls back. Production orgs normally require tests, so validating first is common there.
Source tracking
Scratch orgs and some sandboxes support source tracking. The CLI records which components changed locally and which changed in the org. On those orgs you can run the commands without a selection flag:
sf project deploy start
sf project retrieve startThe CLI then moves only the changed components. Orgs without tracking, such as Developer Edition and production, need an explicit selection flag.
Common errors
- No source-tracking support. The command says tracking is not enabled for the org. Add
--source-dir,--metadata, or--manifest. - Running outside a project. The CLI cannot find
sfdx-project.json. Change into the project root and run the command again. - Wrong org. Components deploy to an unexpected org. Check the default with
sf org display, or pass--target-org. - Missing dependencies. A component refers to another component that is not in the deployment, such as a field used by an Apex class. Include the dependency or confirm the org already has it.
- Test failures. Production deployments that include Apex fail if the required tests fail. Fix the tests or the code, then deploy again.
Exercise
You run sf project deploy start with no flags against a Developer Edition org and the command fails. Why does it fail, and how do you fix it?
Show answer
Developer Edition orgs do not have source tracking, so the CLI does not know which components changed. Add a selection flag such as --source-dir force-app, --metadata ApexClass:MyClass, or --manifest package.xml.
Exercise
What does sf project deploy validate do that sf project deploy start does not?
Show answer
It runs the deployment checks, including required tests, and then rolls back. Nothing is saved to the org, so it confirms a deployment would succeed without changing it.