Foreign Key
A Foreign Key in Salesforce is a field on one object that stores the unique 18-character ID of a record on another object, establishing a relationship between the two.
Definition
A Foreign Key in Salesforce is a field on one object that stores the unique 18-character ID of a record on another object, establishing a relationship between the two. Salesforce never exposes the underlying database constraint and never lets admins define raw foreign keys; instead, the platform creates them automatically when a Lookup or Master-Detail relationship field is defined on an object. The relationship field renders as the parent's record name in the UI and stores the parent's ID under the covers.
Every standard parent-child link in Salesforce uses this pattern. Opportunity.AccountId is the foreign key from Opportunity to Account. Contact.AccountId is the foreign key from Contact to Account. Custom Master-Detail and Lookup fields produce custom foreign keys (My_Object__c.Parent__c) following the same model. Foreign keys also drive SOQL relationship queries (SELECT Account.Name FROM Opportunity) and the polymorphic links on standard fields like Task.WhatId, which can store the ID of multiple parent object types.
How Salesforce's foreign keys differ from a traditional RDBMS
Lookup vs. Master-Detail
Both relationship field types create foreign keys, but the constraint differs. A Lookup is a soft foreign key: deleting the parent does not cascade to the child by default (configurable to either clear the field or block deletion). A Master-Detail is a hard foreign key: deleting the parent cascades to the child, ownership and sharing inherit from the parent, and the child cannot be reparented unless Allow Reparenting is enabled on the field.
Polymorphic foreign keys
Salesforce supports polymorphic relationships where one foreign key can point to records on several different objects. Task.WhatId is the canonical example: it stores the ID of an Account, Opportunity, Case, or any custom object the admin has enabled for Activities. SOQL handles polymorphic FKs with the TYPEOF and WHEN clauses, and Apex resolves them at runtime by inspecting the prefix of the stored ID.
External IDs as alternative keys
Foreign keys reference Salesforce IDs, but data loaded from external systems often uses an external system''s identifier. The External ID field type marks a custom field as a unique alternate key on the object. Upsert operations match on the external ID, find the corresponding Salesforce record, and populate the standard foreign key automatically. Without external IDs, every integration has to do a query-then-write pattern to resolve parent IDs.
Indirect Lookup for external objects
External Objects (Salesforce Connect) use Indirect Lookup fields, a special foreign key that matches an External ID on the parent to a value stored on the child. This is the only foreign key type that does not store a Salesforce ID; it stores the matching external value and joins at query time. Performance is sensitive to the external system's indexing.
SOQL relationship traversal
Once a foreign key exists, SOQL lets queries traverse it with dot notation. SELECT Name, Account.Name, Account.Owner.Email FROM Opportunity walks Opportunity to Account to Account.Owner in one query, generating implicit joins. Up to five levels of upward traversal are supported. Downward traversal (child records from parent) uses subqueries: SELECT Name, (SELECT Name FROM Opportunities) FROM Account.
Foreign keys and sharing
Master-Detail foreign keys are tightly coupled with sharing: the child inherits OWD, role-based sharing, and permission-set object access from the master. Lookup foreign keys do not transfer sharing; each object has its own sharing model and the relationship is purely structural. Designing the relationship type up front is one of the highest-leverage decisions in any data model.
Cascading deletes and orphan handling
Master-Detail cascades: delete the parent and the children disappear. Lookups offer three behaviors on parent delete: Don't Allow Deletion of the Parent, Clear the Lookup Field, or Cascade Delete (available on standard objects only when enabled via Salesforce Support). Choosing the right cascade behavior at field creation time avoids costly cleanup later.
Create a foreign key with a custom relationship field
Salesforce creates the underlying foreign key automatically when an admin adds a Lookup or Master-Detail field. The decision points are the relationship type, the parent object, and the field name.
- Open the child object's Fields page
Setup, Object Manager, pick the child object, click Fields and Relationships, then New.
- Pick Lookup or Master-Detail
Lookup for soft relationships (Account-Contact-like); Master-Detail for tight ownership inheritance (Invoice-Line-Item-like).
- Choose the parent object
Select the object the foreign key points to. Multiple lookups to the same parent are allowed (a deal can have a Primary Account and a Billing Account).
- Configure label, name, and reparenting
Set the field label and API name. For Master-Detail, decide whether Allow Reparenting is on; that decision is hard to reverse.
- Set the parent-delete behavior (Lookup only)
Pick Don't Allow Deletion, Clear the Field, or Cascade Delete. Master-Detail always cascades.
- Add to page layouts and FLS
Add the field to the relevant page layouts and set field-level security on every profile that needs read or edit access.
Lookup or Master-Detail; decides the FK semantics.
The object the foreign key points to.
User-facing label and programmatic identifier.
Don''t Allow, Clear, or Cascade. Default is Clear.
- Master-Detail relationships are hard to convert to Lookup later; the child cannot exist without a parent. Plan the relationship type carefully before populating data.
- Cascade Delete on a Lookup is enabled only by Salesforce Support and only on certain standard objects. Custom-to-custom Lookups cap at Clear or Don''t Allow.
- Polymorphic foreign keys cannot be queried with dot notation across all possible parents; use TYPEOF or query each parent type separately.
- External ID fields can be set to Unique. The constraint applies even when the field is on multiple records with the same value via Person Account/Account dual storage; watch the edge cases.
Trust & references
Cross-checked against the following references.
- Object Relationships OverviewSalesforce Help
- Using Relationships in SOQLSalesforce Developers
Straight from the source - Salesforce's reference material on Foreign Key.
- Relationships Among ObjectsSalesforce Developers
- External ID FieldsSalesforce Developers
About the Author
Dipojjal Chakrabarti is a B2C Solution Architect with 29 Salesforce certifications and over 13 years in the Salesforce ecosystem. He runs salesforcedictionary.com to help admins, developers, architects, and cert/interview candidates sharpen their fundamentals. More about Dipojjal.
Test your knowledge
Q1. What is a Foreign Key in Salesforce?
Q2. What's an example of a foreign key field?
Q3. Why do foreign keys matter for SOQL?
Discussion
Loading discussion…