Sub topic 13
Limits, performance, and when not to use external objects
This chapter assumes you have followed the earlier chapters. You know what an external data source and an external object are, and you can read basic SOQL and Apex. It covers what live access costs, how to reduce the cost, and when to copy the data instead.
The core trade-off: every read is a callout
An external object has no rows stored in Salesforce. When a user opens a list view, a related list, or a record page, or when Apex runs a SOQL query, Salesforce sends a request to the external system and waits for the answer. The external system must be available, fast, and able to handle the load.
This one fact causes most of the limits and performance problems in this chapter. Compare it with a custom object.
| Concern | Custom object (copied data) | External object (virtualized data) |
|---|---|---|
| Data freshness | As fresh as the last sync | Current at query time |
| Salesforce data storage | Counts against storage | Does not count |
| Response time | Salesforce database speed | Network plus external system speed |
| Availability | Salesforce uptime | Salesforce uptime and external uptime |
| Query features | Full SOQL, reports, formulas, automation | Reduced, depends on adapter |
| Load on the source system | None after the sync | Every user action can hit it |
Salesforce publishes limits for external data, such as callout rates per license and Apex callout limits. These change between releases and depend on your edition and licenses. Check the current Salesforce Connect and Apex limits documentation before you size a design. Do not rely on numbers remembered from a blog post.
Where limits come from
Several separate constraints can apply to the same request.
- Salesforce Connect limits. Salesforce limits the volume of external callouts, and the allowance depends on your license.
- Apex governor limits. External queries and writes made from Apex run inside a transaction. That transaction has limits on callouts, total callout time, and the number of SOQL queries and DML statements. A query on an external object can cost a callout as well as a SOQL query, so a loop of queries uses up both budgets quickly.
- Timeouts. If the external system is slow, Salesforce gives up and the user sees an error. A slow source system means a slow page in Salesforce.
- The external system's own limits. The API may have rate limits, per-request row caps, or its own paging rules. Many users opening the same list view multiply the load.
- Adapter capabilities. The adapter decides which filters, sorts, and aggregates can be pushed to the source. The Apex Connector Framework example in the last chapter declared only
ROW_QUERY, and a custom adapter can support less than that.
Feature gaps to plan for
External objects do not support everything that standard and custom objects do. The exact list depends on adapter and release, so verify each item against current documentation before you promise it.
- Sharing. External objects have no record-level sharing model. Access is controlled through object and field permissions, which you set up in the access chapter. If the source data has row-level security, enforce it in the external system, for example through the user identity your credentials pass.
- Reports and analytics. Reporting on external objects is limited compared with internal objects. Because each report run is a live query, large reports are slow and heavy on the source.
- Search. Global search and SOSL on external objects need search enabled on the data source. Search results also come from live callouts, not from a Salesforce index.
- Automation. Do not assume that triggers, flows, formula fields, and roll-up summaries behave as they do on custom objects. Test each one.
- Relationships. External lookup and indirect lookup relationships work, but a related list on a record page makes its own callout each time the page loads.
- History and ownership. There is no ownership field, and Salesforce keeps no field history for data it does not store.
Writing queries that perform
Query behavior is where you have the most control.
Select only the fields you need
Every field you request travels back over the network. A wide SELECT on a list view or in Apex slows the response. Avoid fields you will not use.
Filter and limit at the source
A filter is only useful if the adapter can send it to the source system. The OData adapters translate supported WHERE, ORDER BY, and LIMIT clauses into the request. A custom adapter does the same only if you write it that way. In the Apex Connector Framework, DataSource.QueryUtils.process filters rows in memory after your code has fetched them, which works for small data sets but not for large ones. For large sources, read the query context in your adapter and build the external request from it.
Avoid queries in loops
Each query is a callout. One query per record in a loop will exhaust limits and make users wait. Collect the identifiers first, then run one query.
Set<String> externalIds = new Set<String>{ '1001', '1002', '1003' };
Map<String, Product__x> productsById = new Map<String, Product__x>();
for (Product__x p : [
SELECT ExternalId, Name__c, Price__c
FROM Product__x
WHERE ExternalId IN :externalIds
LIMIT 200
]) {
productsById.put(p.ExternalId, p);
}
for (String id : externalIds) {
Product__x match = productsById.get(id);
if (match == null) {
System.debug('No external product for ' + id);
} else {
System.debug(match.Name__c + ' costs ' + match.Price__c);
}
}This makes one callout instead of three. The pattern matters more as the list grows.
Plan for paging and large result sets
For OData sources with many rows, look at the paging settings on the external data source. Server-driven paging lets the external system decide the page size and return a token for the next page. The data source also has a high data volume option. Both exist because a single response with every row is slow and often impossible. Always put a LIMIT on Apex queries and design list views with filters, so users never ask for the whole table.
Handle failures
A callout can fail even when your code is correct. Catch exceptions where a failure should not stop the whole operation.
try {
List<Product__x> expensive = [
SELECT ExternalId, Name__c, Price__c
FROM Product__x
WHERE Price__c > 100
LIMIT 20
];
System.debug('Fetched ' + expensive.size() + ' products');
} catch (Exception e) {
System.debug('External query failed: ' + e.getMessage());
}Decide what the user should see when the source is down. A clear message is better than a long wait followed by a generic error.
Writes and consistency
Writable external objects add risk. A write goes to the external system immediately, and Salesforce cannot roll it back if a later step in the same transaction fails. Database.insertImmediate returns a result, so always check isSuccess(). Do not assume that a Salesforce transaction rollback will undo an external write. If you need multi-step consistency across both systems, design compensating actions or use a different integration pattern.
Choosing: virtualize, copy, or mix
The following diagram summarizes a decision process. It is a starting point, not a rule.
External objects fit well when:
- The data is large, and users look up a few records at a time.
- Freshness matters more than speed.
- Storage cost or data residency rules make copying undesirable.
- The source system has a stable, well-performing API.
Copying fits better when:
- You need heavy reporting, formulas, roll-ups, or automation on the data.
- Record-level sharing in Salesforce must control access.
- The source system is slow, often offline, or rate limited.
- Many users read the same data constantly, so live access would repeat identical callouts.
A mixed design is common. Copy the small set of fields you report on, such as an account ID and a status, into Salesforce. Keep the bulky details as an external object that opens only when a user needs it. Event-driven approaches such as change notifications from the source can keep the copied subset current without polling.
Common mistakes
- Adding an external related list to a heavily used page layout without testing the load it creates.
- Querying inside a loop and hitting callout or time limits in Apex.
- Assuming a filter reaches the source system when the adapter filters in memory.
- Building reports and dashboards on large external objects and then blaming Salesforce for slowness.
- Forgetting that external objects have no record-level sharing, and exposing sensitive rows to every user with object access.
- Treating a successful external write as part of a Salesforce transaction that can be rolled back.
Exercise
A team puts an external related list of order lines on the Account page. After launch, the page is slow at peak hours and the order system's API logs show spikes. Name two causes tied to how external objects work and two changes that would help.
Show answer
Each page load makes a live callout, so every user opening an Account triggers a request to the order system, and the page cannot render the list faster than that system responds. Peak hours multiply the callouts. Helpful changes include limiting the related list to a few columns and rows, adding filters so the source can narrow results, moving the list to a tab users open on demand, or copying a summary (such as order count and last order date) into Salesforce so the page does not need the live call. If the source is the bottleneck, work with its owners on indexes or API rate limits.
Exercise
An Apex method receives 150 Account records and, for each one, runs a SOQL query on Product__x filtered by one ExternalId. What problem will occur, and how do you fix it?
Show answer
It makes up to 150 queries, and each one is a live callout, so the transaction is likely to hit its limits on callouts, queries, or total time, and it will be slow even if it succeeds. Collect all the ExternalId values into a set first, run one query with WHERE ExternalId IN :ids and a LIMIT, store the results in a map keyed by ExternalId, then look up each record in the map.
Exercise
Give two conditions under which you would copy data into Salesforce instead of using an external object.
Show answer
Examples: you need heavy reporting, roll-ups, formulas, or triggers on the data; Salesforce record-level sharing must control who sees each row; the source system is slow, unreliable, or rate limited; or many users read the same data so often that repeated live callouts waste capacity.