Sub topic 7
Anatomy of an external object
This chapter assumes you know the basic Salesforce data model, have seen SOQL, and have already run Validate and Sync against an external data source. It looks at what Validate and Sync produces: the object, its fields, and how those parts map back to the external system.
What an external object is
An external object is metadata that describes a table in another system. It looks like a custom object, but Salesforce stores no rows for it. When a user opens a record, a list view, or a report, or when Apex runs a query, Salesforce calls the external system and builds the rows from the response.
Three things tell you that you are looking at an external object:
- The API name ends in
__x, for exampleProduct__x. Custom objects end in__c. - The object points to an external data source and to a table in that source.
- Its records are identified by an external key, not by a key that Salesforce owns.
Custom fields you add to an external object still end in __c. So Product__x can have a field Price__c.
The standard fields
Every external object has three fields that Salesforce adds for you. You cannot delete them.
| Field | What it holds | Where it comes from |
|---|---|---|
| `Id` | A Salesforce record ID for the row | Generated by Salesforce when the row is retrieved |
| `ExternalId` | The unique key of the row in the external system | Mapped from the external key column |
| `DisplayUrl` | A URL that points to the record in the external system | Supplied by the adapter, or left empty if the source provides none |
ExternalId
ExternalId is the field that matters most. It must uniquely identify a row in the external table. Salesforce uses it to fetch a single row, to match rows in relationships, and to decide which row an update or delete applies to. It is a text field, even when the key in the source is a number. In OData sources the key column of the entity is mapped to ExternalId.
If the external key is made of several columns, you must expose a single unique value yourself. A common approach is to add a computed column or view in the source system that joins the parts into one string.
Id
Id looks like any other Salesforce ID, with a key prefix that belongs to the external object. Salesforce generates it on retrieval, because the external system has no knowledge of Salesforce IDs. Do not store Id values and expect them to stay valid for later. Store ExternalId when you need a durable reference to a row.
DisplayUrl
DisplayUrl is a URL field. When it has a value, Salesforce can use it to link users to the record in its home system. The value comes from the adapter. Whether it is filled in depends on the adapter and on the external system. With a custom Apex adapter you decide the value yourself, as the DisplayUrl column in the custom connection class does.
The Name field
Every object in Salesforce needs a name field. It labels the record in lookups, list views, and search results. On an external object, you choose which custom text field is the Name Field during Validate and Sync, or later in the object's field list. It is a normal custom field that is flagged as the name. It is not a separate standard field.
Pick a column that a person would recognize, such as a product name or a case title. If you pick an ID-like column, users see opaque values in every lookup.
Custom fields and column mapping
Each custom field on an external object maps to one column in the external table. The field has two names:
- The field name inside Salesforce, such as
Price__c. - The external column name, such as
priceorUnitPrice, which Salesforce sends to the external system.
The external column name must match the source exactly. If the source renames a column, the mapping breaks and queries on that field fail or return empty values. Changing a field label in Salesforce is safe, but changing the external column name is a mapping change.
Validate and Sync converts column types to field types. The exact mapping depends on the adapter, but the usual cases look like this:
| External type | Salesforce field type |
|---|---|
| String | Text, or Long Text Area for long values |
| Integer, decimal | Number |
| Boolean | Checkbox |
| Date and time | Date/Time or Date |
| Link or URI | URL |
Text length matters. A column that holds long values can be created as a short text field, and the text may then be cut off. Check the length on each field after a sync.
Looking at the fields from Apex
You can inspect an external object with the same describe calls you use for any other object. This is a quick way to confirm what the sync created.
Schema.DescribeSObjectResult d = Product__x.SObjectType.getDescribe();
System.debug('Object: ' + d.getName());
for (Schema.SObjectField f : d.fields.getMap().values()) {
Schema.DescribeFieldResult fd = f.getDescribe();
System.debug(
fd.getName() + ' | ' + fd.getType() + ' | createable=' + fd.isCreateable()
);
}On a read-only data source, isCreateable() returns false for the fields. If you later enable writes on the data source, the same call shows which fields accept values.
How external objects differ from custom objects
Because Salesforce does not hold the data, some features that rely on stored rows work differently or are not available. Check the current documentation for your release, but plan for these differences:
- There are no
OwnerId,CreatedDate, orLastModifiedDatefields unless the external system provides columns you map yourself. - Ownership-based sharing does not apply. Access is controlled with object and field permissions, which the next chapter covers.
- Each read is a callout to the external system, so queries are slower than queries on local data and are subject to limits.
- Records are read-only by default. Writing is a separate option on the data source.
- Fields cannot be formula or roll-up summary fields, because those need stored data.
- Reports, search, and list views work, but each one triggers calls to the external system.
Treat this list as a starting point for design, not as a complete reference.
Object-level settings
On the external object's definition page you will find settings that control how the object behaves.
- Label and plural label. These are the names users see. Validate and Sync proposes them from the table name, and you can change them.
- External data source and table name. These tie the object to its source. The table name must match the external table.
- Allow reports and Allow search. These make the object available in reports and global search. Both cause callouts, so enable them only where users need them.
- Deployment status. An object that is in development is visible only to users who have the Customize Application permission. Set it to Deployed once the object is ready for users.
Metadata view
External objects are metadata, so you can deploy them between orgs. A simplified object definition in the Metadata API looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
<deploymentStatus>Deployed</deploymentStatus>
<externalDataSource>ProductsOData</externalDataSource>
<externalName>Products</externalName>
<label>Product</label>
<pluralLabel>Products</pluralLabel>
<nameField>
<label>Name</label>
<type>Text</type>
</nameField>
</CustomObject>The externalDataSource element holds the name of the data source, and externalName holds the table name. The data source itself and its credentials are separate metadata, so deploy them in the same package or create them first in the target org.
Common errors
- Queries return no rows or an error on one field. The external column name no longer matches the source. Compare the field's external column name with the source schema.
- Related records do not match.
ExternalIdis not unique, or its format differs between systems, for example001in one and1in the other. Fix the key in the source or add a computed column. - Users see ID-like values in lookups. The Name Field is set to a key column. Change it to a descriptive column.
- New columns do not appear. Columns added to the source after the first sync need another Validate and Sync run. Review the object after you resync, because a resync can add fields you did not plan to expose.
- An update affects the wrong row, or none. The external system does not treat
ExternalIdas the key it expects.
Exercise
An external object Order__x shows order numbers like ORD-0042 in every lookup field, and users say they cannot tell orders apart. The external table has columns order_no, customer_name, and description. What is the likely cause, and how do you fix it?
Show answer
The Name Field is probably set to the key-style column order_no, so lookups display it. Change the Name Field to a descriptive text field such as description, or map customer_name to a field and make that the name. ExternalId can stay mapped to order_no, because the key and the name are separate settings.
Exercise
A team stores Id values of Product__x records in a custom text field on Account so they can find the product later. Why is this a problem, and what should they store?
Show answer
Id is generated by Salesforce when a row is retrieved, so it is not a durable reference to the external row. They should store ExternalId, which is the unique key from the external system. Better still, they can use an external lookup relationship, which stores that key for them.