Sub topic 10
Relating external data to Salesforce records
This chapter assumes you can already create an external data source and generate external objects with Validate and Sync. It also assumes you know standard lookup relationships and basic Apex. The goal is to link external rows to Salesforce records, or the other way around, without copying either side.
Why relationships work differently
Salesforce record IDs identify records inside Salesforce. External rows have no Salesforce ID that you control. Their identity is the ExternalId field, which holds the key from the external system. External relationships therefore match on values you choose, not on Salesforce IDs.
Master-detail relationships are not available for external objects. You use the two relationship types below.
| Relationship | Child object | Parent object | What is matched |
|---|---|---|---|
| External lookup | Standard, custom, or external object | External object | The child's lookup field value against the parent's `ExternalId` |
| Indirect lookup | External object | Standard or custom object | The child's indirect lookup field value against a unique External ID field on the parent |
External lookup: Salesforce child, external parent
Use this when a Salesforce record should point at a row that lives externally. For example, an Order_Line__c custom object can reference a Product__x row.
- Open the child object in Object Manager.
- Create a new field of type External Lookup Relationship.
- Choose the parent external object, such as
Product__x. - Set field-level security, add the field to page layouts, and save.
The field stores the parent's ExternalId value. The parent's related list and the child's lookup display both read the parent row live from the external system. Because of this, each page that shows the parent causes a callout.
Indirect lookup: external child, Salesforce parent
Use this when an external row carries a business key that matches a Salesforce record. For example, an external Invoice__x has a customer number, and each Account stores the same number.
- On the parent object (Account), create a custom text field.
- Mark it as External ID and Unique. Indirect lookup requires both.
- On the child external object, create a field of type Indirect Lookup Relationship.
- Select the parent object and the parent field to match against.
The child's field value must equal the parent's External ID field value. If no parent has that value, the lookup shows nothing and no error is raised.
Reading across the relationship in Apex
Treat cross-system traversal with care. A reliable pattern is to read the child, collect the key values, and query the parent in one bulk call. This avoids one callout per child row.
List<Order_Line__c> lines = [
SELECT Id, Product__c
FROM Order_Line__c
LIMIT 50
];
Set<String> productKeys = new Set<String>();
for (Order_Line__c line : lines) {
if (line.Product__c != null) {
productKeys.add(String.valueOf(line.Product__c));
}
}
Map<String, Product__x> productsByKey = new Map<String, Product__x>();
for (Product__x p : [
SELECT ExternalId, Name__c, Price__c
FROM Product__x
WHERE ExternalId IN :productKeys
]) {
productsByKey.put(p.ExternalId, p);
}
for (Order_Line__c line : lines) {
Product__x p = productsByKey.get(String.valueOf(line.Product__c));
if (p != null) {
System.debug(line.Id + ' -> ' + p.Name__c);
}
}Common errors
- Related list is empty. The child values do not match the parent keys exactly. Compare whitespace, leading zeros, and case.
- Indirect lookup field cannot select the parent field. The parent field is not both External ID and Unique.
- Pages load slowly. Each related external row is a live request. Show fewer relationship fields on layouts and keep related lists short.
- Wrong choice of type. If the parent is in Salesforce and the child is external, you need indirect lookup. If the parent is external, you need external lookup.
Exercise
An external Invoice__x object has a Customer_Number__c field. Your Accounts have a text field Customer_Number__c. Which relationship type do you create on Invoice__x, and what two attributes must the Account field have?
Show answer
Create an indirect lookup relationship field on Invoice__x that points to Account and matches the Account's Customer_Number__c. The Account field must be marked as External ID and Unique.