Sub topic 13
Object metadata and moving objects between orgs
This chapter assumes you know how to create objects and fields in Object Manager, and that you have used API names with the __c suffix. You should also have the Salesforce CLI (sf) installed and a Salesforce DX project. Earlier chapters covered the data model. Here you will see how an object definition is stored and how to move it from one org to another.
Objects are metadata, records are data
Salesforce separates two kinds of information:
- Data is the records, such as one Account named Acme Corp.
- Metadata is the definition of the structure, such as the Invoice object, its fields, layouts and permission sets.
Metadata moves between orgs through deployments. Data does not. If you build Invoice__c in a sandbox, none of its records appear in production when you deploy it. Only the empty object definition arrives. Moving records needs a separate tool, such as Data Loader or the Data Import Wizard.
What a custom object looks like as metadata
In a Salesforce DX project, each custom object has its own folder. The object definition lives in a file named after the object's API name:
<?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>This is the file Invoice__c.object-meta.xml. The elements map to settings you already know from Object Manager:
| Element | Meaning |
|---|---|
| `label`, `pluralLabel` | The names users see. They can change without breaking code. |
| `nameField` | The standard Name field and its type (Text or Auto Number). |
| `deploymentStatus` | `Deployed` makes the object visible to users. `InDevelopment` hides it. |
| `sharingModel` | The org-wide default, for example `ReadWrite` or `Private`. |
The API name comes from the file and folder name, not from the label. Each custom field gets its own file in a fields subfolder, for example fields/Amount__c.field-meta.xml. Page layouts, record types and permission sets are separate metadata files that reference the object by API name.
Moving an object between orgs
A common flow is to build in a sandbox, store the definition in version control, and deploy it to the next org.
Retrieve the object and its fields into your project:
sf project retrieve start --metadata CustomObject:Invoice__c --target-org MySandboxDeploy the project's metadata to another org:
sf project deploy start --source-dir force-app --target-org MyProdYou can also use a change set, which is a point-and-click bundle of components. Change sets only work between orgs that are connected, such as a sandbox and its production org. They are harder to review and cannot be kept in version control, so teams usually prefer the CLI.
Common errors
- Missing dependencies. A field that points to another object fails if that object is not in the target org. Deploy or include the parent object first.
- Permissions not deployed. The object exists in the target org, but users cannot see it. Object and field access comes from permission sets and profiles, so include them in the deployment.
- Retrieving only the object.
CustomObject:Invoice__cbrings the fields with it in DX source format. A narrower retrieve, such as one field, brings only that field. - Deleting is not automatic. Deploying does not remove components that you deleted locally. Removing a component needs a destructive changes manifest.
- Production test rules. Deploying Apex code to production requires test coverage. Deploying only objects and fields does not.
Summary
Objects are metadata files that you can retrieve, store in Git and deploy. Records stay behind. A good deployment includes the object, its fields, any layouts and record types, and the permissions that grant access.
Exercise
You deployed Invoice__c and its fields from a sandbox to production. Users in production report that they cannot see the Invoice tab, and the object has no records. Name two reasons that explain this and what you would deploy or load to fix each.
Show answer
The records were not deployed because deployments move metadata, not data, so load them separately with Data Loader or the Import Wizard. Users also have no access because the permission set or profile that grants object and field access was not included, so deploy the permission set and assign it to the users. The tab setting, which is also part of that permission metadata, may need deploying too.