Sub topic 11
Querying External Objects
This chapter assumes you know basic SOQL, you have an external object generated by Validate and Sync, and users already have access to it. You will query that object with SOQL and Apex and see what happens behind the scenes.
How a query becomes a live request
A regular object query reads rows stored in Salesforce. An external object query reads nothing local. Salesforce Connect translates the SOQL into a request for the external system, such as an OData call. It then maps the response back into records.
Every query is a callout, so each one costs network time and counts against limits on the external side.
Querying with SOQL
The object name ends in __x. Standard fields ExternalId and DisplayUrl are always present. Custom fields end in __c.
SELECT ExternalId, DisplayUrl, Name__c, Price__c
FROM Product__x
WHERE Price__c > 100
ORDER BY Name__c
LIMIT 20The WHERE, ORDER BY, and LIMIT clauses are normally sent to the external system, so it filters and sorts the rows. This keeps the response small. Select only the fields you need, because each extra field adds to the payload.
Querying with Apex
Apex uses inline SOQL, the same as for any other object.
List<Product__x> products = [
SELECT ExternalId, Name__c, Price__c
FROM Product__x
WHERE Price__c > 100
LIMIT 20
];
for (Product__x p : products) {
System.debug(p.Name__c + ' costs ' + p.Price__c);
}Each row is read from the external system when the query runs. Nothing is cached in Salesforce, so running the same query twice makes two callouts.
Reading the ID fields
| Field | What it holds | Typical use |
|---|---|---|
| `ExternalId` | The row's key in the external system | Matching rows, indirect lookups, filtering by key |
| `DisplayUrl` | A link to the record in the external system | Showing a link in the UI |
| `Id` | A Salesforce ID generated for the row | Referencing the row in Salesforce, such as in external lookups |
The Id value is created by Salesforce when it retrieves the row. Store ExternalId if you need a stable reference to the external record.
Common errors and gotchas
- Unsupported query features. Some adapters cannot evaluate every clause. The OData adapters, for example, may reject certain joins or aggregate functions. Test the exact query you plan to run.
- Filtering on fields the external system cannot filter. The adapter may fail, or fetch more rows than expected.
- Slow or unavailable external system. A slow endpoint makes your query slow. An unavailable one makes it fail, so handle the exception in Apex.
- Callouts in transactions. An external object query is a callout, so it counts against Apex callout limits and must respect the rules for callouts in the same transaction as pending DML. Check the Salesforce documentation for the current limits.
- Large result sets. Always add a
WHEREclause and aLIMITfor external objects. Avoid unbounded queries.
Handling failures in Apex
Wrap the query in a try/catch block when the external system may be down.
try {
List<Product__x> items = [
SELECT ExternalId, Name__c
FROM Product__x
LIMIT 10
];
System.debug(items.size() + ' rows returned');
} catch (Exception e) {
System.debug('External query failed: ' + e.getMessage());
}Exercise
Why does running the same SOQL query twice against Product__x make two callouts, and what two things can you do in the query to reduce the cost of each one?
Show answer
External object rows are not stored in Salesforce, so each query is read from the external system when it runs, with no local copy to reuse. To reduce the cost, select only the fields you need and add a selective WHERE clause with a LIMIT, so the external system returns fewer rows and less data.