Sub topic 8
Converting between formats with the Salesforce CLI
This chapter assumes you know the layout of both formats: a package.xml manifest with monolithic files in Metadata API format, and decomposed files under a package directory in source format. You also need a project with sfdx-project.json and the Salesforce CLI (sf) installed.
What conversion does
Conversion is a local file operation. The CLI reads files from one folder and writes the other layout into a different folder. It does not contact an org and it does not deploy anything.
The two directions use different subcommands, and the subcommand names the format you are converting from.
| Direction | Command | Input | Output |
|---|---|---|---|
| Source to Metadata API | `sf project convert source` | Package directory in source format | Folder with `package.xml` and monolithic files |
| Metadata API to source | `sf project convert mdapi` | Folder with `package.xml` | Package directory in source format |
Source format to Metadata API format
Run this from the project root, where sfdx-project.json lives:
sf project convert source --root-dir force-app --output-dir mdapi_outputThe command recombines each decomposed object folder into one Account.object file. It also writes a generated package.xml that lists every component type it found. If you want to convert only part of the project, narrow the input with --manifest, --source-dir, or --metadata:
sf project convert source --manifest manifest/package.xml --output-dir mdapi_output
sf project convert source --source-dir force-app/main/default/classes --output-dir mdapi_output
sf project convert source --metadata ApexClass --output-dir mdapi_outputMetadata API format to source format
Point the command at the folder that holds package.xml:
sf project convert mdapi --root-dir mdapi_output --output-dir force-appThe command splits Account.object into Account.object-meta.xml and one file per field under fields/. Run it inside a project, because the CLI uses sfdx-project.json to decide where files belong.
Deploying the converted output
A converted Metadata API format folder can go straight to an org:
sf project deploy start --metadata-dir mdapi_output --wait 10--metadata-dir tells the CLI that the folder is in Metadata API format. Without it, deploy start treats your files as source format and reads them from the package directories in sfdx-project.json.
Verifying a conversion
A conversion can finish without errors and still leave something out. Check three things after every run:
- Open the generated
package.xmland confirm it lists the types you expect. - Count the files. List both folders and compare the component names.
- Convert the output back to the original format and compare it with what you started with.
sf project convert source --root-dir force-app --output-dir mdapi_output
sf project convert mdapi --root-dir mdapi_output --output-dir roundtrip
diff -r force-app/main/default roundtripDifferences in formatting or file order are possible. Look for missing files. If diff reports a component that exists only on one side, find out why before you rely on the converted output.
Exercise
You run sf project convert mdapi --root-dir mdapi_output --output-dir force-app. Which format is the input in, and what happens to a file named Account.object?
Show answer
The input is in Metadata API format, because the subcommand names the source format. The CLI decomposes Account.object into Account.object-meta.xml plus one file per field, such as fields/Rating_Notes__c.field-meta.xml, under the output directory.
Exercise
A teammate converts a project and finds that one Apex class is missing from the output. Name two things to check.
Show answer
Check whether a .forceignore rule matches the class, and check whether the class lives in a folder that is not listed under packageDirectories in sfdx-project.json. If you used --manifest or --metadata, also check that the filter includes the class.