Sub topic 1
Metadata Types and Components
Prerequisites
This chapter assumes you have the following background:
- You know what Apex classes, custom objects, and custom fields are in Salesforce.
- You can read XML and move around a project directory.
- You can run basic command-line and Git commands.
You do not need to have used the Salesforce CLI yet. Later chapters cover the CLI commands. This chapter builds the vocabulary that the rest of the tutorial depends on: metadata types, components, and the two ways Salesforce writes them to disk.
What metadata is
Salesforce stores two kinds of information in an org. Data is the records your users create, such as a particular Account named "Acme". Metadata is the configuration that describes how the org behaves and what it looks like. Apex classes, custom objects, custom fields, page layouts, flows, and permission sets are all metadata.
When you build in Salesforce with code and version control, you work with metadata as files. You retrieve it from an org to your computer, edit it, store it in Git, and deploy it to another org. The files must follow a known layout so the tools can read them. Salesforce supports two layouts, and this tutorial compares them.
Metadata types and components
Two terms matter in every later chapter.
- A metadata type is a category of configuration.
ApexClass,CustomObject,CustomField,Layout, andFloware metadata types. Each type has a defined shape: the properties it can hold and the way it is stored. - A metadata component is one concrete instance of a type. The Apex class
MyClassis a component of typeApexClass. TheAccountobject is a component of typeCustomObject. The fieldAccount.Rating_Notes__cis a component of typeCustomField.
A useful comparison is a class and its objects in programming. The type is the definition, and each component is one instance. Every component is identified by its type plus its full name. The type and name together are the component's identity throughout retrieve, deploy, and conversion.
| Metadata type | Example component | Full name |
|---|---|---|
| `ApexClass` | An Apex class | `MyClass` |
| `CustomObject` | The standard Account object | `Account` |
| `CustomField` | A custom field on Account | `Account.Rating_Notes__c` |
| `Layout` | A page layout | `Account-Account Layout` |
Notice that names of components that belong to a parent use a dotted or hyphenated form. A custom field is always tied to an object, so its full name includes the object name. Remember this naming rule. It is how the same field can be addressed as its own component and also be found inside an object.
Types can contain other types
Some types are children of another type. A custom field, a validation rule, and a record type all belong to an object. The Metadata API lets you treat them in two ways:
- As part of the parent. Retrieving
CustomObjectnamedAccountcan return the object together with its fields and other children. - As separate components. You can name
CustomFieldmembers individually and work with only those fields.
This parent and child relationship is the main reason the two file layouts differ. The layouts differ in how they write parent components that have many children.
Two layouts for the same metadata
Salesforce exposes the same set of components in two on-disk layouts.
- Metadata API format is the layout the Metadata API reads and writes. A manifest file called
package.xmldescribes the contents. Each component is stored as one file, and a custom object becomes one largeAccount.objectfile that contains its fields and other children. - Source format is the layout used by Salesforce DX projects. Large files are decomposed into many smaller files. A custom object becomes a folder with a small object file and one file per field.
Both layouts describe identical components. The difference is the arrangement of files, not the content of your org.
How a component looks in each layout
The examples below use one Apex class and the Account object, taken from the plan for this tutorial.
Metadata API format
package.xml
classes/
MyClass.cls
MyClass.cls-meta.xml
objects/
Account.object
Source format
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.xmlBoth layouts store the Apex class the same way. The class has a source file, MyClass.cls, and a companion file, MyClass.cls-meta.xml, that holds settings such as the API version. The difference shows up for the object. In Metadata API format, Account.object is one file. In source format, Account is a folder, and the field Rating_Notes__c has its own .field-meta.xml file.
A field in source format 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>The root element, CustomField, is the metadata type. The fullName identifies the component. The remaining elements are the properties that the type defines. In the Metadata API layout this same CustomField block would sit inside the CustomObject element of Account.object.
Why the layout differences matter
You can ignore the layout when you only click through Setup. Once files are involved, the choice affects daily work.
- Version control. If an object with fifty fields is one file, two developers who each add a field both edit the same file, and merges conflict. One file per field keeps diffs small and isolated.
- Deployment tooling. The Metadata API, change sets, and many older tools expect the manifest-driven layout.
- Readability. A small file for each component is easier to review than a single file of thousands of lines.
Later chapters cover each layout in depth, the manifest, the conversion commands, and how to choose. For now, keep the central idea: a type is a category, a component is an instance, and the two layouts are two ways to write the same components to disk.
Seeing types and components in a manifest
A manifest names components by type and member. The package.xml below selects two components.
<?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>Read it as a list of metadata types. Inside each <types> block, <name> is the metadata type and each <members> entry is the full name of a component of that type. This manifest asks for the Apex class MyClass and the custom object Account. The <version> element sets the API version used for the retrieve or deploy.
Two details are easy to miss:
- The element named
<name>holds the type, not the component. The component names go in<members>. - A single
<members>value of*selects every component of the type, for types that support it. The next chapters return to this.
Common misunderstandings
A few mistakes appear often when people first meet these terms.
- Confusing a type with a component.
CustomObjectis a type.Accountis a component. A manifest needs both: the type in<name>and the component in<members>. - Assuming the two layouts hold different content. They do not. Converting between them rearranges files. It does not add or remove configuration.
- Assuming every component is a single file. Some components need a companion file, and some are bundles of several files. Later chapters explain companion
-meta.xmlfiles, bundles, and static resources. - Treating a child as always separate. A field can be a standalone file in source format and a nested element in Metadata API format. Both describe the same component.
Summary of the working model
Use this model as you read the rest of the tutorial.
- Metadata is org configuration. It is divided into types, and each type has many components.
- A component is identified by its type and full name.
- Parent components such as custom objects can have child components such as fields.
- Metadata API format writes each parent as one file and uses
package.xmlto say what is included. - Source format decomposes parents into folders and small files, and uses
sfdx-project.jsonto mark the project. - The Salesforce CLI converts between the two layouts without changing the components.
Exercise
A manifest must retrieve the Apex class MyClass and the custom object Account. In the <types> block for the class, which element holds ApexClass and which holds MyClass? Also, is Account.Rating_Notes__c a type or a component?
Show answer
<name> holds the metadata type, so it contains ApexClass. <members> holds the component's full name, so it contains MyClass. Account.Rating_Notes__c is a component, an instance of the CustomField type, and its full name includes the parent object.
Exercise
A teammate says that converting a project from Metadata API format to source format deletes the custom fields from the object because Account.object disappears. Is that correct? Explain what happens to the fields.
Show answer
It is not correct. The two layouts describe the same components. In Metadata API format the fields are nested inside the single Account.object file. In source format they are decomposed into separate files such as objects/Account/fields/Rating_Notes__c.field-meta.xml. Conversion rearranges the files and keeps the configuration.