Sub topic 5
Retrieving metadata by directory or name
This chapter covers the two simplest ways to pull metadata from an org into a Salesforce DX project: by source directory and by metadata name. It assumes you have a DX project with sfdx-project.json, an authorized org set as the default target, and basic Git knowledge. Manifest-based retrieval is covered in a later chapter.
What a retrieve does
sf project retrieve start asks the org for metadata, converts it to source format, and writes it into your project. Source format splits large components into small files. For example, a custom object becomes a folder with one file per field.
The command needs two decisions from you: which components to ask for, and which org to ask. You can pick components in three ways:
- With
--source-dir, you name local paths. The CLI works out which components those paths represent. - With
--metadata, you name components by type and name. - With
--manifest, you point to apackage.xmlfile. This is the later chapter.
The org comes from --target-org, or from the default target org if you leave the flag out.
Retrieving by metadata name
The --metadata flag (short form -m) takes values of the form Type:Name. The type is the metadata API name, such as ApexClass or CustomObject. The name is the component's full name, including the __c suffix for custom objects.
sf project retrieve start --metadata ApexClass:AccountService --metadata CustomObject:Invoice__cRepeat the flag for each selection, as above. You can also list several values after one flag, separated by spaces.
These are the forms you will use most:
| Value | What it retrieves |
|---|---|
| `ApexClass:AccountService` | One Apex class |
| `ApexClass` | Every Apex class in the org |
| `ApexClass:Account*` | Every Apex class whose name starts with `Account` |
| `CustomObject:Invoice__c` | The object and its child components, such as fields |
A bare type name retrieves everything of that type. That can be thousands of files in an established org, so use it deliberately.
The * wildcard matches any run of characters at the end of a name. Quote the value when you use it, or your shell may try to expand it as a file glob:
sf project retrieve start --metadata 'ApexClass:Account*' --metadata 'ApexTrigger:Account*'Where the files land
For each retrieved component, the CLI first checks whether a file for it already exists in your project. If one does, the CLI overwrites it in place. If none does, the CLI writes it to the default package directory, which is the entry in sfdx-project.json with "default": true. A project with several package directories can therefore get a new file in a different place than you expected. Move it afterward if needed.
Child components and folders
Some types have child components. When you retrieve CustomObject:Invoice__c, you get its fields, record types, and similar pieces along with the object. Other types live in folders in the org, such as reports, dashboards, and email templates. To retrieve a foldered component, include the folder in the name, for example Report:SalesReports/Pipeline.
Retrieving by source directory
The --source-dir flag (short form -d) takes a path in your project. The CLI looks at the components that live under that path, then retrieves the org's current version of each one.
sf project retrieve start --source-dir force-app/main/default/classesYou can pass a directory or a single file. You can also repeat the flag:
sf project retrieve start \
--source-dir force-app/main/default/classes/AccountService.cls \
--source-dir force-app/main/default/objects/Invoice__cThe important rule is that a source-directory retrieve only refreshes components you already have locally. It can't discover components that exist only in the org. If a teammate added InvoiceService in the org and you don't have it, retrieving force-app/main/default/classes won't bring it down. To get it, name it with --metadata ApexClass:InvoiceService.
Choosing between the two
| Situation | Better choice |
|---|---|
| The component isn't in your project yet | `--metadata` |
| You want to refresh files you already have | `--source-dir` |
| You want everything of one type | `--metadata Type` |
| You know the path but not the metadata type | `--source-dir` |
You can combine both flags in one command. The CLI retrieves the union of the two selections.
Choosing the org
Every retrieve goes to one org. Without --target-org, the CLI uses the value set by sf config set target-org. To override it for a single command, pass an alias or username:
sf project retrieve start --metadata ApexClass:AccountService --target-org my-sandboxCheck which default you are using before a retrieve that overwrites files:
sf config get target-orgA retrieve from the wrong org is easy to do and quiet when it happens. It overwrites your local files with another org's versions and reports success.
Source tracking and conflicts
Scratch orgs and some sandboxes have source tracking. The CLI compares your local files against the org's revision history and notes what changed on each side. In those orgs, running sf project retrieve start with no selection flags retrieves everything that changed in the org since your last sync.
Tracking also protects you. If a component changed both locally and in the org, the retrieve stops with a conflict error instead of overwriting your edit. You can bypass that with --ignore-conflicts, which lets the org's version win:
sf project retrieve start --metadata ApexClass:AccountService --ignore-conflictsCommit or stash your local work first. With the flag on, a retrieve can discard uncommitted edits and Git is your only way back. A later chapter covers conflict resolution in more detail.
Previewing a retrieve
To see what a retrieve would change before running it, use the preview command:
sf project retrieve previewIt lists remote changes that would be retrieved, local changes that would conflict, and files that .forceignore would exclude. It works in orgs with source tracking.
Reading the result
A successful retrieve prints a table of components with their type, name, and local file path. Treat that table as your record of what happened. Then check what Git sees:
git status
git diff --statA clean diff after a retrieve means local and org already matched. Unexpected changes in files you didn't select usually mean the org had been edited directly, or a different API version formatted the file differently.
Common errors
- No components in the result. The name probably has the wrong case or type, such as
ApexClass:accountservicewhen the class has a different case, orObject:Invoice__cinstead ofCustomObject:Invoice__c. Check the type name against the metadata API. - A conflict error. Local and remote both changed. Compare the two, decide which wins, then retry with
--ignore-conflictsor deploy your local version first. - The wrong org's files appear. The default target org wasn't what you thought. Run
sf config get target-org, then revert with Git. - A file you expected is missing after a retrieve. A pattern in
.forceignoremay match it. Preview shows ignored files. - The command fails with a timeout message. Large retrieves can take longer than the wait time. The org keeps working, but the CLI stops waiting. Raise the wait with
--wait(minutes), or retry with narrower selections. - The shell expands
*. Quote any value containing a wildcard.
Exercises
Exercise
A teammate created the Apex class InvoiceService in the shared sandbox. You run sf project retrieve start --source-dir force-app/main/default/classes and the class doesn't appear. Why, and what command fixes it?
Show answer
A source-directory retrieve only refreshes components that already exist locally, so it can't find a class that is new to your project. Retrieve it by name instead: sf project retrieve start --metadata ApexClass:InvoiceService --target-org my-sandbox.
Exercise
You want every Apex class whose name starts with Billing. Write the command so the shell doesn't expand the wildcard.
Show answer
sf project retrieve start --metadata 'ApexClass:Billing*'
Exercise
You retrieve CustomObject:Invoice__c with the default target org still pointing at an old sandbox, and local files change unexpectedly. How do you confirm the cause and recover?
Show answer
Run sf config get target-org to see which org the command used. Then run git status and git diff to see the changes, and revert them with Git (for example, git restore force-app) if they were not committed. Set the correct org with sf config set target-org my-sandbox and retrieve again.