Sub topic 5
Retrieving metadata by directory or name
This chapter assumes you have a Salesforce DX project, an authorized org set as the default target (my-sandbox in the examples), and a basic idea of metadata types such as ApexClass and CustomObject. Retrieving copies the org's version of components into your local project. It is the first half of the round trip that deployment completes.
What retrieve does
The command is sf project retrieve start. It asks the org for components, converts them to source format, and writes them under a package directory listed in sfdx-project.json. If a local file already exists for a retrieved component, the retrieve overwrites it.
You choose what to retrieve with one of two selectors:
--metadata(-m) names components by type and name.--source-dir(-d) points at local paths whose components should be refreshed from the org.
Retrieving by metadata name
Use Type:Name pairs. Repeat the flag for each component.
sf project retrieve start --metadata ApexClass:AccountService --metadata CustomObject:Invoice__cThis retrieves one Apex class and one custom object from the default org. To target a different org, add --target-org my-sandbox.
Two variations are useful:
- Give only the type, such as
--metadata ApexClass, to retrieve every component of that type. - Use a wildcard in the name, such as
--metadata "ApexClass:Account*". Quote the value so your shell does not expand the asterisk.
Names are case-sensitive in practice. Custom objects and fields need their API suffixes, for example Invoice__c.
Retrieving by source directory
Pass a path inside your project to refresh the components stored there:
sf project retrieve start --source-dir force-app/main/default/classesThe CLI maps each file in that directory to a component, then retrieves the org's current version of those components. A directory retrieve only refreshes components you already have locally. It does not discover components that exist in the org but not in your project. For that, use --metadata, which can name components you have never retrieved.
| Selector | Best for | Finds components not yet local? |
|---|---|---|
| `--metadata` | Pulling specific or new components | Yes |
| `--source-dir` | Refreshing what you already track | No |
Common errors
- No components found. The type or name is misspelled, or the component does not exist in the target org. Check you are pointing at the org you expect.
- Files not appearing. The component may match a pattern in
.forceignore, which keeps it out of the project. - Conflicts. In an org with source tracking, a retrieve can stop if the same component changed both locally and in the org. A later chapter covers resolving this.
After a retrieve, run git status and git diff. The diff shows exactly what changed in the org compared with your last commit, which makes retrieve a useful way to see what teammates or admins changed.
Exercise
You need the Apex class BillingService, which exists in the org but has never been in your project. Should you use --source-dir or --metadata, and what is the command?
Show answer
Use --metadata, because a directory retrieve only refreshes components that already exist locally. The command is sf project retrieve start --metadata ApexClass:BillingService.