Sub topic 10
Relating External Data to Salesforce Records
This chapter assumes you can create custom fields and relationships in Setup, write basic SOQL, and read short Apex. You should also have an external object generated by Validate and Sync. A relationship is what lets an external row appear on a Salesforce record page, or the reverse.
Why external objects need special relationships
Standard lookup and master-detail relationships join two objects by Salesforce record ID. Rows in an external object have no Salesforce ID. They are identified by the standard ExternalId field, whose value comes from the external system.
Salesforce therefore offers two relationship types built for this case:
- External lookup: the parent is an external object.
- Indirect lookup: the child is an external object and the parent is a standard or custom Salesforce object.
Both are lookup-style relationships. Both match on a value instead of a Salesforce ID.
External lookup relationships
An external lookup links a child object to a parent external object. The child can be a standard, custom, or external object. The relationship field on the child holds a foreign key value. Salesforce matches that value against the parent's ExternalId field.
Suppose Product_x is an external object and OrderLinex is another external object from the same system. Each order line row has a column such as productid. Setting up the relationship looks like this:
- In Setup, open Object Manager and select OrderLine__x.
- Under Fields & Relationships, create a new field of type External Lookup Relationship.
- Choose Product__x as the related external object.
- Name the field and its relationship. In this example, the field is Product_c and the relationship name is Product_r.
- For an external child object, enter the External Column Name. This is the column on the external system that holds the product's key, for example product_id.
- Add the field to page layouts and set field-level security.
For a standard or custom child object, you add the external lookup field to a Salesforce object. The field stores the external ID value as text. For example, you could add a Product_c field to a custom SupportCase_c object so that each case points to a row in Product_x.
After setup, a child record page shows the parent as a clickable link. The parent record page can show a related list of children if you add it to the layout.
Indirect lookup relationships
An indirect lookup goes the other way. The child is an external object, and the parent is a standard or custom object in your org. Indirect lookup works with the OData adapters and custom Apex adapters. Check the current adapter support list before you design around it.
Salesforce cannot match a Salesforce record ID against an external column, so you pick a field on the parent to match against. That field must be a custom field with both the External ID and Unique attributes. The unique check must be case sensitive, because the match is case sensitive. A value of acme-001 in the external system does not match ACME-001 on the parent.
Suppose Account has a custom field SAPIdc. It is set to Unique (case sensitive) and External ID. The external object Invoice_x has a column holding the same SAP identifier. To relate them:
- Open Invoice__x in Object Manager and create a field of type Indirect Lookup Relationship.
- Select Account as the parent object.
- Select SAPId_c as the Target Field.
- Enter the External Column Name, the column on the external system whose values match SAPId_c.
- Name the field and relationship, for example Account__c.
An Account record page can now list its invoices in a related list. Salesforce looks up the account's SAPId_c value and asks the external system for invoices with that value.
Comparing the relationship types
| Relationship | Child object | Parent object | Matches on |
|---|---|---|---|
| Lookup (standard) | Standard, custom | Standard, custom | Salesforce record ID |
| External lookup | Standard, custom, external | External | Child foreign key to parent ExternalId |
| Indirect lookup | External | Standard, custom | Child column to parent's unique external ID field |
| Master-detail | Not supported with external objects | Not supported with external objects | Not applicable |
A standard lookup field cannot point at an external object. Master-detail relationships cannot involve external objects either. As a result, you do not get roll-up summary fields or cascade delete across the boundary. If you need a count of invoices on an account, you must compute it yourself, for example with a SOQL COUNT() query.
Querying across relationships
For an external lookup, you can traverse the relationship in SOQL with the __r name:
SELECT ExternalId, Quantity__c, Product__r.Name__c
FROM OrderLine__x
WHERE Quantity__c > 5
LIMIT 20Whether a given traversal works depends on the adapter and the external system. Test each query shape you plan to ship. Each traversal may become extra requests to the external system.
An indirect lookup field holds the parent's external ID value, not a Salesforce ID. In Apex, you query the parent first and use its external ID to filter the child:
public with sharing class InvoiceService {
public static List<Invoice__x> invoicesForAccount(Id accountId) {
Account acct = [
SELECT Id, SAP_Id__c
FROM Account
WHERE Id = :accountId
LIMIT 1
];
if (String.isBlank(acct.SAP_Id__c)) {
return new List<Invoice__x>();
}
return [
SELECT ExternalId, Amount__c
FROM Invoice__x
WHERE Account__c = :acct.SAP_Id__c
LIMIT 50
];
}
}The blank check avoids sending a filter on an empty value. Each run of this method makes one query on Account and one live callout for Invoice__x.
Common errors and gotchas
- Related list is empty. The usual cause is mismatched values. Compare the external column with the parent field character by character. Look for case differences, leading zeros, and trailing spaces.
- Parent field cannot be selected as the target. The field is missing the External ID or Unique attribute, or it is not a custom field. Standard fields such as Account Name cannot be used.
- Parent link does not render. The user lacks read access to the parent object, or the lookup field is not visible through field-level security.
- Relationship breaks after the external schema changes. If the external column is renamed or removed, the field no longer resolves. Run Validate and Sync again, then fix the External Column Name on the relationship field.
- Duplicate matches. Salesforce guarantees uniqueness of the parent field only. Nothing in the external system stops it from holding many child rows per key. That is expected for one-to-many data. A key that is missing on the external side leaves the parent with no children.
Design guidance
- Choose the relationship direction first. If a Salesforce record should show external rows, use an indirect lookup. If external rows should reference other external rows, use an external lookup.
- Decide which field is the join key before you build. For an indirect lookup, adding a unique external ID field to a large parent object, such as Account, is a data model change. Plan for loading that field across existing records.
- Keep keys stable and consistent in format. Normalize case in the external system or the parent field so matching works.
- Remember that a relationship does not copy data. If a report needs fields from both sides, you may need to bring the data into Salesforce rather than join it live. The limits chapter covers this.
Exercise
Your org stores customer accounts in Salesforce. Each account has a custom text field BillingCodec. A warehouse system exposes shipments as an external object Shipmentx, and each shipment row has a column customercode with the same codes as BillingCodec. Which relationship type do you create, on which object, and what must be true of BillingCode__c?
Show answer
Create an indirect lookup relationship field on Shipment_x with Account as the parent object and BillingCode_c as the target field. Set customercode as the External Column Name. BillingCode_c must have the External ID attribute and the Unique attribute with case sensitivity, because the match is case sensitive.
Exercise
A teammate asks you to make Product__x the master in a master-detail relationship with a custom object so they can use a roll-up summary field. Is this possible, and what is an alternative?
Show answer
It is not possible, because master-detail relationships are not supported with external objects. Use an external lookup from the custom object to Product__x. For the aggregate, compute it in SOQL or Apex, or copy the needed data into Salesforce.