How to retrieve salesforce objects with package.xml
Using following syntax we retrieve any Standard or Custom Salesforce objects.
Syntax to retrieve objects:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<name>CustomObject</name>
<members>Object_API_Name</members>
<members>Account</members>
<members>Case</members>
</types>
<version>62.0</version>
</Package> Object metadata definition contains following items:
- Compact layouts
- Actions
- Buttons & Actions
- Fields Sets
- Fields details
- Dependent Fields/ Controlling Fields
- Record Types
- Validations Rules
Find how to retrieve apps metadata using package.xml.
Find how to retrieve Utility Bar metadata using package.xml.
What is CustomObject in Salesforce?
The CustomObject metadata type represents an object together with its customizations. You can retrieve your own custom objects (names ending in __c) and the customizations you made on standard objects such as Account or Case.
Steps to retrieve it in VS Code
- Open your Salesforce DX project in VS Code and authorize your org with SFDX: Authorize an Org.
- Open manifest/package.xml, or create the file, and paste the XML shown above.
- Right-click package.xml and choose SFDX: Retrieve Source in Manifest from Org.
- When the command finishes, open the folder
force-app/main/default/objectsto see the retrieved files.
Use the API name of the object in each members tag. Standard objects use their plain name (Account, Case) and custom objects end with __c.
Retrieve it with the Salesforce CLI
If you prefer the terminal, run one of these commands from your project folder:
sf project retrieve start --manifest manifest/package.xml
sf project retrieve start --metadata CustomObject:Account Where the files are saved
After a successful retrieve, the files are placed inside your project, for example in force-app/main/default/objects/Account/Account.object-meta.xml.
Common problems and fixes
- Entity of type CustomObject cannot be found: check the spelling and the API name of the member, and make sure you are authorized to the correct org.
- Nothing was retrieved: run
sf org listto confirm the default org, and check that the component actually exists there. - API version: the examples use version 62.0. You can use a higher version if your org supports it.
FAQ
Does retrieving an object also retrieve its fields?
Yes. Fields, record types, validation rules, compact layouts and field sets come down as separate files inside the object folder.
Do I get every standard field of Account?
You get the customizations you made, such as custom fields, record types and validation rules. Standard fields are not all listed unless you request them.