Sub topic 13
Object metadata and moving objects between orgs
This chapter assumes you know how to create a custom object in Object Manager, that you recognize API names such as Invoice__c, and that you have used Setup in a sandbox or Developer Edition org. You do not need to know the Salesforce CLI yet. The chapter explains what the CLI does as it goes.
Objects are metadata, records are data
Salesforce separates two kinds of information.
- Data is the set of records, such as the Account named "Acme Corp".
- Metadata is the definition of the structure that holds data: objects, fields, layouts, record types, permission sets, and so on.
When you click New in Object Manager and create Invoice__c, you create metadata. When a user later saves an invoice, that is data. The distinction matters when you move work between orgs. Deploying metadata creates the empty structure in the target org. It does not copy any records.
Metadata is stored as structured definitions that you can read, edit, and keep in version control. This is what makes it possible to build in a sandbox and promote the result to production without redoing the clicks.
The anatomy of a custom object file
In a Salesforce DX project, each object lives in a folder under force-app/main/default/objects. The folder name is the object's API name. The object's own settings are in a file named <ApiName>.object-meta.xml.
Here is a minimal definition of the Invoice__c object:
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Invoice</label>
<pluralLabel>Invoices</pluralLabel>
<nameField>
<label>Invoice Number</label>
<type>Text</type>
</nameField>
<deploymentStatus>Deployed</deploymentStatus>
<sharingModel>ReadWrite</sharingModel>
</CustomObject>Each element maps to something you set in the UI:
| Element | Meaning |
|---|---|
| `label` and `pluralLabel` | The names users see. They can be translated and changed without breaking code. |
| `nameField` | The standard Name field of the object. `Text` gives you free text. `AutoNumber` gives you generated values such as INV-0001. |
| `deploymentStatus` | `Deployed` makes the object visible to users. `InDevelopment` hides it from them while you build. |
| `sharingModel` | The organization-wide default for record access, for example `Private`, `ReadWrite`, or `ControlledByParent`. |
Notice what is absent. The file does not contain the API name, because the file and folder names supply it. It also does not contain any fields. Each custom field has its own file.
Where fields and other pieces live
Under the object folder, related metadata is split into subfolders:
force-app/main/default/objects/Invoice__c/
├── Invoice__c.object-meta.xml
├── fields/
│ ├── Account__c.field-meta.xml
│ ├── Amount__c.field-meta.xml
│ └── Status__c.field-meta.xml
├── recordTypes/
├── listViews/
└── validationRules/Splitting the definition into one file per component keeps version-control diffs small. Two people who add different fields touch different files, so they rarely conflict.
Some components that relate to your object do not live in its folder. Page layouts are in layouts/, permission sets are in permissionsets/, and tabs are in tabs/. A layout file is named like Invoice__c-Invoice Layout.layout-meta.xml.
Standard objects in metadata
You do not define standard objects like Account, but you can customize them. The same folder structure applies. If you add a custom field to Account, the project contains objects/Account/fields/Region__c.field-meta.xml. There is no Account.object-meta.xml unless you also changed a setting on the object itself and retrieved it.
This is a common source of confusion. Retrieving a custom field on a standard object brings only that field. It does not bring the whole Account definition, and it does not overwrite Account in the target org.
Ways to move metadata between orgs
You have several options. They differ in how much control, repeatability, and tracking they give you.
| Method | Best for | Main limitation |
|---|---|---|
| Change sets | Admins moving a small set of components from a sandbox to a connected org | Manual selection, no version history, only between related orgs, and cannot delete components |
| Salesforce CLI with a DX project | Teams using version control and automation | Needs setup and some command-line skill |
| Unlocked packages | Modular, versioned releases of a feature | Needs package design and dependency planning |
| Metadata API tools (for example, IDE plugins) | One-off retrievals and deployments | Easy to deploy the wrong thing without source control |
Change sets are common for small teams. The CLI approach is the foundation for most modern workflows, so the rest of this chapter focuses on it.
Retrieving and deploying with the Salesforce CLI
Assume you have a DX project and you have authorized two orgs with aliases dev-sandbox and prod. The commands below show the standard cycle.
Retrieve the object and its fields from the sandbox:
sf project retrieve start \
--metadata CustomObject:Invoice__c \
--target-org dev-sandboxRetrieving CustomObject:Invoice__c brings the object definition along with its fields, record types, list views, and validation rules. Layouts, tabs, and permission sets are separate metadata types, so retrieve those explicitly:
sf project retrieve start \
--metadata "Layout:Invoice__c-Invoice Layout" \
--metadata "CustomTab:Invoice__c" \
--metadata "PermissionSet:Invoice_Access" \
--target-org dev-sandboxBefore you deploy for real, run a validation. This runs the same checks as a deployment but does not save anything:
sf project deploy validate \
--source-dir force-app \
--target-org prod \
--test-level RunLocalTestsWhen validation passes, deploy:
sf project deploy start \
--source-dir force-app \
--target-org prod \
--test-level RunLocalTestsDeployments to production run Apex tests. If your code has less than 75 percent coverage overall, or any test fails, the deployment fails and nothing is saved. A deployment is all or nothing by default.
A manifest file for selective deploys
Folder-based commands deploy everything in the path. A manifest file, usually named package.xml, lists exactly which components to move.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Invoice__c</members>
<name>CustomObject</name>
</types>
<types>
<members>Invoice__c.Status__c</members>
<name>CustomField</name>
</types>
<types>
<members>Invoice_Access</members>
<name>PermissionSet</name>
</types>
<version>60.0</version>
</Package>You then deploy with sf project deploy start --manifest package.xml --target-org prod. Use a manifest when you want to ship a precise set of changes and nothing else.
Dependencies and ordering
Metadata components reference each other, and the target org must be able to resolve every reference.
- A lookup field on
Invoice__cpoints toAccount. Account is standard, so it exists everywhere. - If
Invoice__chad a lookup to another custom object, such asProject__c, that object must already exist in the target org or be part of the same deployment. - A layout that shows a field fails to deploy if the field is missing.
- A permission set that grants access to
Invoice__c.Status__cneeds that field to exist.
A single deployment handles ordering for you, as long as everything is included. Errors appear when you leave something out, and the message usually names the missing component, such as Field Project__c does not exist.
Edge cases and common errors
Records do not travel with metadata. A deployment moves structure only. To copy records, use Data Loader, the sf data commands, or data import tools. When records reference each other, load parents before children and map the new IDs. Record IDs differ between orgs.
Deleting is separate. Deploying a project that no longer contains a field does not remove the field from the target. You must supply a destructive changes manifest, destructiveChanges.xml, to delete components. Even then, Salesforce blocks deletion of a field that is referenced by a layout, formula, flow, or Apex code.
Changing field types is restricted. Some changes are rejected once data exists, such as converting a lookup to master-detail when child records have an empty parent value. Plan relationship choices early.
Master-detail adds constraints. Adding a required master-detail field to an object that already has records in the target org fails, because existing records have no parent. Create it as a lookup, populate it, and then convert it.
API names must match. A deployment treats Invoice__c and Invoice1__c as different objects. A mismatch in the API name between orgs creates a new object instead of updating the existing one.
Picklist values are merged, not replaced. Depending on the field, deployed picklist values may be added without removing extras that exist in the target.
Managed package namespaces. Components from a managed package have a prefix such as acme__Invoice__c. You cannot edit most of their metadata, and your deployment must not try to.
Checking the result
After a deployment, confirm the structure exists in the target org. You can use the same schema describe call you used in earlier chapters:
Schema.DescribeSObjectResult invoiceDescribe = Invoice__c.SObjectType.getDescribe();
System.debug(invoiceDescribe.getName() + ' custom? ' + invoiceDescribe.isCustom());If Invoice__c does not exist, this code does not compile in the target org. That failure is a quick signal that the deployment did not include the object.
Summary
- Objects, fields, layouts, and permission sets are metadata. Records are data and do not move with a metadata deployment.
- A custom object is defined in an
object-meta.xmlfile, and each field, record type, and validation rule has its own file in a subfolder. - Change sets suit small admin tasks. The CLI and a version-controlled DX project suit repeatable team workflows.
- Retrieve the object, its layout, its tab, and its permission set together, because they are separate metadata types.
- Validate before you deploy, include every dependency, and use
destructiveChanges.xmlto delete components.
Exercise
You built Invoice__c with three custom fields in a sandbox and deployed it to production with the CLI. Users in production can open the Invoices tab, but they see no data, and the three fields are missing from the page layout. Name two likely causes and how you would fix each.
Show answer
First, records are data, not metadata, so the deployment created an empty object. Load the needed records with Data Loader or sf data commands. Second, the layout and field access were probably not deployed. Layouts and permission sets are separate metadata types from the object. Retrieve the layout and the permission set from the sandbox, then deploy them. Also confirm that the permission set is assigned to the users and grants field-level access.
Exercise
Your DX project no longer contains Notes__c, a field you removed in the sandbox. After deploying to production, the field is still there. Why, and what should you do?
Show answer
A normal deployment only adds or updates components. It never deletes components that are missing from the source. Create a destructiveChanges.xml manifest that lists Invoice__c.Notes__c as a CustomField, and deploy it. If the deployment fails, check whether a layout, formula, flow, or Apex class still references the field, and remove those references first.