Sub topic 8
Converting between formats with the Salesforce CLI
This chapter assumes you can read a package.xml manifest, you know the difference between Metadata API format and source format, and you can run commands in a terminal. It covers the two conversion commands, what they do to your files, and how to check that a conversion worked.
Why conversion exists
Salesforce keeps metadata in two on-disk layouts. Metadata API format is the layout that the Metadata API reads and writes. It has one file per component, plus a package.xml manifest. Source format is the layout Salesforce DX projects use. It splits large components into many small files, and an sfdx-project.json file describes the project.
The two layouts hold the same information. Conversion is the step that reshapes files from one layout to the other. You need it when:
- A team moves an older Metadata API format codebase into a DX project.
- A DX project must feed a tool that only understands Metadata API format.
- You want to inspect what a deployment will actually send to the org.
The Salesforce CLI (sf) does this with the sf project convert command family.
The two commands
# Source format -> Metadata API format
sf project convert source --root-dir force-app --output-dir mdapi_output
# Metadata API format -> source format
sf project convert mdapi --root-dir mdapi_output --output-dir force-appThe names describe the input. convert source takes source format and produces Metadata API format. convert mdapi takes Metadata API format and produces source format.
| Command | Input | Output |
|---|---|---|
| `sf project convert source` | Source format directory, such as `force-app` | Metadata API format directory with a generated `package.xml` |
| `sf project convert mdapi` | Metadata API format directory containing `package.xml` | Source format directory |
Both commands read from --root-dir and write to --output-dir. They do not contact an org. Conversion is a local file operation, so it needs no authentication and it is safe to repeat.
What source to Metadata API conversion does
Run convert source from the root of a DX project, the folder that contains sfdx-project.json. The CLI looks up the package directories in that file and finds the components under them.
Take this project layout:
sfdx-project.json
force-app/main/default/
classes/
MyClass.cls
MyClass.cls-meta.xml
objects/
Account/
Account.object-meta.xml
fields/
Rating_Notes__c.field-meta.xmlAfter conversion the output directory looks like this:
mdapi_output/
package.xml
classes/
MyClass.cls
MyClass.cls-meta.xml
objects/
Account.objectThree things happened:
- The
force-app/main/defaultnesting disappeared. Metadata API format puts each type folder directly under the root. - The decomposed object was merged. The small
Account.object-meta.xmlfile and every file underfields/were combined into a singleAccount.objectfile. - A
package.xmlwas generated. It lists each type and member that the converter found, and it uses thesourceApiVersionfromsfdx-project.json.
The Apex class files are copied as they are. A class is already one body file plus one companion file, so there is nothing to merge.
Limiting what gets converted
By default the command converts every package directory. You can narrow it. Use --package-name to convert a single package, --manifest to convert only the components a manifest lists, or --metadata to name types or members on the command line.
# Convert only what an existing manifest lists
sf project convert source --manifest manifest/package.xml --output-dir mdapi_output
# Convert only two Apex classes
sf project convert source --metadata ApexClass:MyClass --metadata ApexClass:OtherClass --output-dir mdapi_outputThe generated package.xml then reflects only the components you selected. Confirm flag names with sf project convert source --help, because the CLI adds and renames options between releases.
What Metadata API to source conversion does
Run convert mdapi against a directory that holds a package.xml. It reads the manifest and files, then writes the same components in source format.
Starting from this input:
mdapi_output/
package.xml
classes/
MyClass.cls
MyClass.cls-meta.xml
objects/
Account.objectthe output is the decomposed layout. Account.object becomes a folder named Account. Its fields are written to fields/, one file each, and the remaining object settings stay in Account.object-meta.xml. The suffix changes from .field content inside one file to a .field-meta.xml file per field.
Where the files land depends on --output-dir. If you point it at an existing project folder, such as force-app, the CLI writes into that tree and may overwrite files that have the same path. Convert into an empty folder first when you are unsure, then compare.
The conversion does not create sfdx-project.json. If you are starting a new DX project from legacy files, create the project first (sf project generate --name my-project), then convert into its force-app directory.
The manifest that travels with the output
The generated package.xml is a normal manifest. Here is a minimal one that matches the earlier example.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>MyClass</members>
<name>ApexClass</name>
</types>
<types>
<members>Account</members>
<name>CustomObject</name>
</types>
<version>60.0</version>
</Package>Notice that the manifest names Account once under CustomObject. It does not list Rating_Notes__c separately in this form, because the field is inside the Account.object file. A manifest can also name fields as their own CustomField members, such as Account.Rating_Notes__c. Both styles are valid, and they select different amounts of metadata when you retrieve or deploy.
The <version> value is the API version. On a source-to-Metadata conversion it comes from sourceApiVersion, shown here:
{
"packageDirectories": [
{ "path": "force-app", "default": true }
],
"namespace": "",
"sourceApiVersion": "60.0"
}If sourceApiVersion is missing, the CLI falls back to a default that may not match your org. Set it explicitly so the output is predictable.
How the pieces connect
The diagram shows the data flow. Conversion is local on both sides. Only deployment and retrieval touch the org.
Deploying a converted directory
You do not have to convert before every deployment. sf project deploy start reads source format directly and converts internally. You convert by hand when a tool or process needs the Metadata API layout as a file on disk.
sf project deploy start --metadata-dir mdapi_output --wait 10--metadata-dir tells the CLI the directory is already in Metadata API format. --wait 10 makes the command wait up to ten minutes for the result instead of returning a job ID right away.
Verifying a conversion
Do not assume a conversion is correct because the command exited without an error. Check it.
- Read the manifest. Open the generated
package.xmland confirm each type and member you expected is there. A missing member means the converter did not find the component. - Count components. List the type folders in the output. The number of classes, triggers, and objects should match the input.
- Inspect a decomposed component. Open
Account.objectand search forRating_Notes__c. The field must appear inside it. - Round trip. Convert source to Metadata API format, then convert that output back into a fresh folder. Compare it with the original using
git diff --no-index. - Validate against an org. Run a check-only deployment of the converted directory:
sf project deploy start --metadata-dir mdapi_output --dry-run --wait 10A dry run validates the deployment without saving changes. It catches problems such as a missing dependency or a bad API version.
A round trip is not always byte-identical. The CLI may reorder elements inside a file or change whitespace. Treat a diff that shows only ordering as harmless, and a diff that shows removed elements as a real problem.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `convert source` finds no components | Run outside the project, or `packageDirectories` points to the wrong path | Run from the folder with `sfdx-project.json`, and check each `path` |
| A file is missing from the output | `.forceignore` excludes it | Review `.forceignore` patterns |
| Class has no version or status | The `-meta.xml` companion file is missing | Add the `MyClass.cls-meta.xml` file |
| Output overwrote local edits | `--output-dir` pointed at a populated folder | Convert into an empty folder, and use Git to review |
| Deploy uses an unexpected API version | `sourceApiVersion` not set | Set it in `sfdx-project.json` |
Summary
sf project convert source merges decomposed source files into Metadata API format and writes a package.xml. sf project convert mdapi splits Metadata API files into source format. Both are local and repeatable. Verify by reading the manifest, inspecting a merged object, and running a check-only deployment.
Exercise
You run sf project convert source --root-dir force-app --output-dir mdapi_output and the result contains objects/Account.object but no fields folder. Is the field Rating_Notes__c lost? How do you check, and where does the API version in the generated package.xml come from?
Show answer
The field is not lost. Metadata API format stores custom fields inside the single Account.object file, so no fields folder exists. To check, open Account.object and look for a <fields> element whose <fullName> is Rating_Notes__c. The <version> in the generated package.xml comes from sourceApiVersion in sfdx-project.json.
Exercise
You have legacy files in legacy/ with a package.xml and an objects/Account.object file. Write the commands to create a DX project called my-project and convert the legacy files into its force-app directory.
Show answer
sf project generate --name my-project
then, from the folder that contains both my-project and legacy:
sf project convert mdapi --root-dir legacy --output-dir my-project/force-app