Sub topic 5
Decomposition: how a custom object is split into many files
This chapter assumes you know what a custom object and custom field are, can read XML, and can run basic Git and command-line commands. It also assumes you have seen the two layouts from earlier chapters: the Metadata API format described by package.xml, and the source format used by Salesforce DX projects.
Decomposition is the main structural difference between the two layouts. It explains why one object can be a single file in one layout and a folder of many files in the other.
What decomposition means
In Metadata API format, a custom object is one file. Account.object holds the object's settings, all its custom fields, list views, validation rules, record types, and more. For an object with a long history, this file can run to tens of thousands of lines.
In source format, the Salesforce CLI splits that file into one small file per child component. The object file keeps only the object-level settings. Each field, list view, validation rule, and so on gets its own file in a subfolder.
Decomposition changes only the layout on disk. The org holds the same metadata either way. The CLI splits the file when it retrieves or converts, and it merges the pieces back when it deploys or converts in the other direction.
Why Salesforce splits the file
A single large file causes three practical problems for teams:
- Merge conflicts. Two developers who each add a field to
Account.objectedit the same file, often near the same lines. Git may report a conflict even though the changes are unrelated. - Unreadable diffs. A one-field change appears inside a huge file, and reviewers must hunt for it.
- Unstable ordering. The Metadata API returns elements in a server-determined order, so unrelated lines can shift between retrieves.
With one file per field, adding a field creates a new file. Changing a field's label changes only that field's file. Two developers working on different fields never touch the same file.
What a decomposed object looks like
Here is the same object in each layout. The custom field Rating_Notes__c on Account is the example.
Metadata API format
objects/
Account.object <- object settings + every field,
list view, validation rule, ...
Source format
objects/
Account/
Account.object-meta.xml <- object-level settings only
fields/
Rating_Notes__c.field-meta.xml
Region__c.field-meta.xml
listViews/
All_Accounts.listView-meta.xml
validationRules/
Require_Industry.validationRule-meta.xml
recordTypes/
Partner.recordType-meta.xmlThe folder is named after the object. Each child type has a folder named for the type, and each child file is named after the component plus a type suffix and -meta.xml.
Common child folders include:
| Child component | Folder | File suffix |
|---|---|---|
| Custom field | `fields/` | `.field-meta.xml` |
| List view | `listViews/` | `.listView-meta.xml` |
| Validation rule | `validationRules/` | `.validationRule-meta.xml` |
| Record type | `recordTypes/` | `.recordType-meta.xml` |
| Compact layout | `compactLayouts/` | `.compactLayout-meta.xml` |
| Field set | `fieldSets/` | `.fieldSet-meta.xml` |
| Business process | `businessProcesses/` | `.businessProcess-meta.xml` |
| Sharing reason | `sharingReasons/` | `.sharingReason-meta.xml` |
| Index | `indexes/` | `.index-meta.xml` |
| Button or link | `webLinks/` | `.webLink-meta.xml` |
Folders appear only when the object has at least one component of that type. An object with no validation rules has no validationRules/ folder.
The conversion flow
The following diagram shows the direction of each operation and what happens to the file.
What the files contain
The object-level file keeps only settings that belong to the object itself, such as its sharing model, label, and feature flags. The exact contents depend on the object. For a standard object like Account, it is often short.
A child file is a complete XML document with its own root element. Compare this field file with the <fields> element it came from in Account.object.
<?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>In the monolithic file, the same content sits inside the object as a <fields> element:
<fields>
<fullName>Rating_Notes__c</fullName>
<label>Rating Notes</label>
<type>Text</type>
<length>100</length>
<required>false</required>
</fields>The element name changes from fields to CustomField, and the xmlns attribute is added so the file is a valid standalone document. The CLI does this rewrite for you in both directions. You never edit it by hand during conversion.
Seeing decomposition happen
You can watch the split by converting a small project. This example assumes you have a Metadata API format directory in mdapi_output that contains objects/Account.object.
sf project convert mdapi --root-dir mdapi_output --output-dir force-app
find force-app -path "*objects/Account*" -type f | sortTypical output for an object with one custom field, one list view, and one validation rule looks like this:
force-app/main/default/objects/Account/Account.object-meta.xml
force-app/main/default/objects/Account/fields/Rating_Notes__c.field-meta.xml
force-app/main/default/objects/Account/listViews/All_Accounts.listView-meta.xml
force-app/main/default/objects/Account/validationRules/Require_Industry.validationRule-meta.xmlRunning the reverse command turns these four files back into one Account.object:
sf project convert source --root-dir force-app --output-dir mdapi_outputHow package.xml interacts with decomposition
Decomposed children are still addressable as their own metadata types. A manifest can ask for the whole object or for just one child, and the choice changes what you retrieve.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Account.Rating_Notes__c</members>
<name>CustomField</name>
</types>
<version>60.0</version>
</Package>This manifest retrieves only the one field. The member name uses the Object.Component pattern. Compare it with the earlier manifest that listed Account under CustomObject: that one retrieves the object together with its child components, which in source format lands as the whole objects/Account/ folder.
In a Metadata API format retrieve, a field-only request still returns an Account.object file, but that file contains only the requested field and the minimum object wrapper. In source format, the CLI writes that field into fields/Rating_Notes__c.field-meta.xml.
Edge cases and gotchas
- Partial object files in Metadata API format. A package that lists only some children produces an
Account.objectthat is not the full object. Deploying it does not delete the other fields. Metadata deploys add and update components. They do not remove components that are absent from the package. - Deleting a field. Removing
Rating_Notes__c.field-meta.xmlfrom your source folder does not delete the field in the org on the next deploy. Deletions need a destructive changes manifest or the CLI's delete support for tracked orgs. - Standard objects decompose too.
Account,Contact, and other standard objects get the same folder layout. Their object file often has few settings, and the folder mostly holds your custom additions. - Other types decompose as well. Some other types are split in source format, for example workflows and sharing rules. Check the files the CLI produces for your project instead of assuming a type stays in one file.
- Folder name is the object API name. For a custom object, that is the full name including
__c, such asInvoice__c. Renaming the folder by hand breaks the link between the children and their parent. - Line endings and ordering. Files generated by the CLI have stable element ordering, which keeps diffs small. Hand-edited files that reorder elements are legal but produce noise in later retrieves.
Verifying a decomposition
After any convert or retrieve, check three things:
- Every child you expected has its own file under the right type folder.
- The object-level file does not contain
<fields>,<listViews>, or other child elements. - Converting back and comparing against the original gives the same components, even if the element order differs.
sf project convert source --root-dir force-app --output-dir roundtrip
grep -c "<fullName>" roundtrip/objects/Account.objectThe count printed by grep should match the number of <fullName> elements in the original Account.object. A count that differs usually means a child file was lost, skipped by .forceignore, or placed in the wrong folder.
Exercise
A teammate says that deleting fields/Rating_Notes__c.field-meta.xml from the source folder and deploying will remove the field from the org. Is that correct? Name the file in Metadata API format where this field would have been defined, and describe how the field would be removed.
Show answer
It is not correct. Deploys add and update components but do not remove ones that are missing from the project. In Metadata API format the field is defined inside objects/Account.object, as a <fields> element. To remove it from the org, include it in a destructive changes manifest (for example, CustomField with member Account.Rating_Notes__c) or use the CLI's tracked delete support, and then also remove the file from source control.