Sub topic 6
Companion -meta.xml files
This chapter assumes you know what a metadata component is, can read XML, and have seen both the Metadata API format and the source format layouts. It explains the -meta.xml suffix and which files need a companion file.
What a companion file is
Some metadata is not XML. An Apex class is source code, and a static resource is an arbitrary file such as a zip or an image. Salesforce still needs to store settings for these components, such as the API version or the status. Those settings go in a second file next to the content file. The second file has the same name plus the suffix -meta.xml.
classes/
MyClass.cls
MyClass.cls-meta.xmlMyClass.cls holds the code. MyClass.cls-meta.xml holds the settings. The two files form one component, and you must keep them together.
What the companion file contains
For an Apex class, the companion file holds the API version and the status.
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>The root element is the metadata type name. The class body is not repeated here. If you edit the code, you change only the .cls file. If you change the API version, you change only the companion file.
Which components need one
The rule depends on the component, so the table lists common cases.
| Component | Content file | Companion file |
|---|---|---|
| Apex class | `MyClass.cls` | `MyClass.cls-meta.xml` |
| Apex trigger | `MyTrigger.trigger` | `MyTrigger.trigger-meta.xml` |
| Static resource | `logo.png` or `logo.zip` | `logo.resource-meta.xml` |
| Custom field | none | `Rating_Notes__c.field-meta.xml` |
| Custom object | none | `Account.object-meta.xml` |
Two patterns appear in the table.
- Components with a content file (classes, triggers, static resources, Visualforce pages) need a companion file for their settings.
- Components that are pure XML have no separate content file. In source format their only file ends in
-meta.xml, such asRating_Notes__c.field-meta.xml. Here the suffix marks the file as Salesforce metadata. It does not mean there is a second file to look for.
Differences between the two formats
In Metadata API format, a pure XML component uses its type suffix alone, such as Account.object. Components with content still carry a -meta.xml companion, so the .cls plus .cls-meta.xml pair looks the same in both layouts.
In source format, every XML-only component gets the -meta.xml suffix. This is why the same custom object appears as Account.object in one layout and Account.object-meta.xml in the other. Conversion renames these files for you.
Common errors
- Missing companion file. If you add
NewClass.clsand forgetNewClass.cls-meta.xml, deploy fails because the component has no API version. Create both files together. - Mismatched names. The companion name must match the content file name exactly, including case.
myclass.cls-meta.xmldoes not pair withMyClass.cls. - Renaming only one file. When you rename a class, rename both files and update the class name inside the code.
- Editing the wrong file. Code changes belong in the content file. Settings such as
apiVersionandstatusbelong in the companion file.
Check your understanding
Exercise
You add Helper.cls to the classes folder of a source-format project and run a deploy. It fails because the component is incomplete. What is missing, and what should it contain at minimum?
Show answer
The companion file Helper.cls-meta.xml is missing. It needs an ApexClass root element in the metadata namespace with at least an <apiVersion> value, and normally <status>Active</status>.
Exercise
A custom field in source format is stored as Rating_Notes__c.field-meta.xml. Does this field also need a separate content file? Explain.
Show answer
No. A custom field is pure XML, so the -meta.xml file is the whole component. Only components with non-XML content, such as Apex classes, triggers, and static resources, have a separate content file.