Sub topic 5
Decomposition: how a custom object is split into many files
This chapter assumes you can read the two layouts from the earlier chapters: the Metadata API format with its package.xml, and the Salesforce DX source format. It also assumes you know what a custom object, a custom field, and a validation rule are.
The problem decomposition solves
In Metadata API format, one component is one file. A custom object such as Account becomes a single objects/Account.object file. That file contains every field, list view, record type, validation rule, and compact layout for the object.
For a busy object this file grows to thousands of lines. Two developers who each add one field both edit the same file, and Git reports a merge conflict even though the changes are unrelated.
Source format fixes this with decomposition. The CLI splits a large file into a small parent file plus one file for each child component. A child is a component that only exists inside a parent, such as a field inside an object.
What the split looks like
The Metadata API layout and the source layout for the same object look like this:
Metadata API format
objects/
Account.object
Source format
force-app/main/default/objects/
Account/
Account.object-meta.xml
fields/
Rating_Notes__c.field-meta.xml
listViews/
All_Accounts.listView-meta.xml
recordTypes/
Partner.recordType-meta.xml
validationRules/
Name_Required.validationRule-meta.xmlThe object gets a folder named after it. The Account.object-meta.xml file keeps only the object's own settings, such as its label and sharing model. Each child type gets a subfolder, and each child gets one file named <ApiName>.<type>-meta.xml.
Reading a decomposed child file
Each child file is a complete XML document with the child type as its root element. A custom field looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Rating_Notes__c</fullName>
<label>Rating Notes</label>
<type>Text</type>
<length>100</length>
<required>false</required>
</CustomField>If you change length from 100 to 255, Git shows a one-line diff in one small file. Another developer's new field is a separate file, so the two changes do not conflict.
Decomposition is reversible
Nothing is lost in the split. When you deploy source format, or convert it back to Metadata API format, the CLI reassembles the pieces into one Account.object file. You can check this yourself:
sf project convert source --root-dir force-app --output-dir mdapi_output
ls mdapi_output/objectsThe output directory contains a single Account.object, even though force-app holds many files for it.
Which types are decomposed
Custom objects are the most common case. A few other types are split the same way, including Workflow, which becomes separate files for alerts, field updates, and rules. Most types, such as Apex classes and Lightning bundles, are not decomposed. They already map to one component per file or folder.
Common mistakes
- Editing the wrong place. If you convert to Metadata API format and then edit
Account.object, those edits do not flow back intoforce-app. Treat the converted folder as generated output. - Missing child files. A field file must sit in
fields/under its object's folder. Placed anywhere else, the CLI cannot tell which object it belongs to. - Expecting one file per object in a search. To find a field's definition, search for
Rating_Notes__c.field-meta.xml, not for the object file.
Exercise
Two developers each add a different custom field to Account. In Metadata API format they get a merge conflict, but in source format they do not. Why?
Show answer
In Metadata API format both fields live inside the single Account.object file, so both developers edit the same file and Git cannot merge the overlapping changes automatically. In source format decomposition gives each field its own file under objects/Account/fields/, so the two changes touch different files and merge cleanly.