Sub topic 6
Generating external objects with Validate and Sync
An external object is the Salesforce-side definition of a table that lives in another system. You rarely build these by hand. Validate and Sync reads the external system's schema and creates the external objects and fields for you. This chapter covers what that button does, how to run it, what it creates, and what usually goes wrong.
This chapter assumes you know the basic Salesforce data model, can navigate Setup, and have already created an external data source and its authentication. Both are covered in earlier chapters.
What Validate and Sync does
Validate and Sync is a button on the detail page of an external data source. It does two jobs in order.
- Validate. Salesforce makes a callout to the external system using the data source's URL and authentication. For an OData adapter, it requests the service's metadata document. If the callout fails, you get an error and nothing else happens.
- Sync. Salesforce lists the tables the external system exposes. You pick the ones you want, and Salesforce creates an external object for each. For each column it creates a custom field on that object.
The sync reads only the schema. No row data is copied into Salesforce. Rows are fetched later, when someone queries the object.
Running it
You need the Customize Application permission to create external objects. Before you start, check that the external data source's authentication works. If the data source uses a named credential, confirm the credential is authenticated for the user running the sync. A per-user credential that has not been set up for you is a common cause of failure.
- In Setup, open External Data Sources and click the name of your data source.
- Click Validate and Sync.
- If validation succeeds, Salesforce shows the tables it found. Select the tables you want.
- Click Sync.
- Open External Objects in Setup to confirm the new objects exist.
Select only the tables you need. Every table you sync adds an object to your org, and external objects count against the org's limit on external objects. Unused objects also clutter permission sets and the object manager. You can run the sync again later to add more tables.
What gets created
For each selected table, the sync creates one external object. Salesforce adds the __x suffix to its API name. A table called Product becomes Product__x.
The sync also creates fields.
| Item | Where it comes from | Example |
|---|---|---|
| Object API name | Table name plus `__x` | `Product__x` |
| `ExternalId` | Standard field holding the row's unique key in the external system | `1042` |
| `DisplayUrl` | Standard field holding a URL for the row | `https://example.com/items/1042` |
| Custom fields | One per column, with `__c` appended | `Price__c` |
| Name field | One text column used as the record's name | `Name__c` |
Each generated field records the name of its source column in the External Column Name setting. The object records its source table in External Table Name. These two settings are how Salesforce translates your SOQL into the right request later. If someone renames a column in the external system, the field in Salesforce still points at the old name and queries on it fail.
Type mapping
Salesforce maps external types to field types. Strings become text fields, numbers become number fields, dates become date or date/time fields, and so on. Columns whose types have no equivalent are skipped rather than failing the whole sync. If a column you expected is missing from the object, its type is the first thing to check. Very long text may also map to a text area, not a plain text field, which affects how it can be filtered.
The name field
Every external object needs one field marked as the name field. Salesforce uses it as the record title in list views, search results, and lookup pickers. The sync chooses one for you, and its guess is not always the best column. After syncing, open the object, go to the field definitions, and check which field has Name Field selected. A product name is a good choice. A numeric code is not.
Syncing again
You can run Validate and Sync again whenever the external schema changes. A repeat sync updates the objects you already have and picks up new columns as new fields. Two points matter here.
- A repeat sync is the supported way to refresh the object definitions. Do not re-create the objects by hand.
- Columns removed from the external system do not make Salesforce rewrite your configuration to match. Review fields that no longer have a matching column, and remove references to them from layouts, list views, and code.
Salesforce stores your customizations of an external object, such as labels, layouts, and permission set entries, in the org, not in the external system. Treat those as yours to maintain.
Sync with a custom adapter
When the data source uses a custom Apex adapter, the sync does not read an OData document. Salesforce calls the sync() method of your DataSource.Connection class and builds objects from the tables and columns it returns. The same Validate and Sync button drives it.
override global List<DataSource.Table> sync() {
List<DataSource.Column> columns = new List<DataSource.Column>();
columns.add(DataSource.Column.text('ExternalId', 255));
columns.add(DataSource.Column.url('DisplayUrl'));
columns.add(DataSource.Column.text('Name', 255));
columns.add(DataSource.Column.text('Category', 255));
List<DataSource.Table> tables = new List<DataSource.Table>();
tables.add(DataSource.Table.get('Product', 'Name', columns));
return tables;
}The first argument to DataSource.Table.get is the table name, which becomes the object. The second names the column to use as the name field. The columns ExternalId and DisplayUrl are required, and the adapter must return them for each table. A later chapter builds the full adapter.
Checking the result
After a sync, run a small query to confirm the object reads real data. Use a low LIMIT so the callout stays small.
SELECT ExternalId, DisplayUrl, Name__c
FROM Product__x
LIMIT 5You can run this in the Developer Console query editor or in Apex. If rows come back, the whole chain works: authentication, the data source, the generated object, and the column mapping. If it fails, the error message often names the column or the HTTP status from the external system.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Validation fails with an authentication error | Named credential or external credential is not authenticated for your user, or the principal is not granted to your permission set | Test the credential, and grant the principal in your permission set |
| Validation fails with a connection or certificate error | Wrong URL, firewall blocking Salesforce, or untrusted certificate | Open the service URL from outside the org and compare it to the data source setting |
| Validation succeeds but the table list is empty | The service exposes no entity sets you can read, or you are pointing at the wrong service root | Confirm the URL ends at the service root, not at one entity |
| A table does not appear or cannot be selected | The table has no usable key, or its name collides with an existing object | Check the key definition in the external metadata, and rename the conflicting object |
| A column is missing from the object | Its type is unsupported | Expose the column as a supported type in the external system, then sync again |
| Queries fail after the sync worked | A column was renamed or removed externally | Sync again, then update the stale field and anything that uses it |
If an OData service has a very large schema, the sync callout can be slow or time out. In that case, ask the service owner for a smaller service document, or use a custom adapter that describes only the tables you need.
What comes next
The sync leaves you with objects that nobody except admins can see. The next steps are to understand the object's structure in detail, grant access through permission sets, and add tabs, layouts, and list views.
Exercise
You run Validate and Sync on an OData data source. The sync succeeds and creates Order__x, but the Discount__c field you expected is missing, and later a teammate reports that queries selecting Customer__c fail because the source team renamed that column. Explain the likely cause of each problem and what you would do.
Show answer
The missing Discount__c field most likely comes from a column whose type has no Salesforce equivalent, because the sync skips such columns. Check the column's type in the external metadata, expose it as a supported type, and run Validate and Sync again. The failing Customer__c field still has the old name in its External Column Name setting, so Salesforce asks the external system for a column that no longer exists. Run the sync again to pick up the renamed column as a new field, then update layouts, list views, and code to use it and remove the stale field.
Exercise
Name the two standard fields every external object has, and say what each one holds.
Show answer
ExternalId holds the unique key of the row in the external system. DisplayUrl holds a URL for the row, which Salesforce uses to link to it.