Skip to content
Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Full Foreign Key entry
How-to guide

How to create a foreign key in Salesforce

You never create a raw foreign key in Salesforce. You create a relationship field, and the platform builds and manages the key for you. Here is how to add one to an object.

By Dipojjal Chakrabarti · Founder & Editor, Salesforce DictionaryLast updated Jun 16, 2026

You never create a raw foreign key in Salesforce. You create a relationship field, and the platform builds and manages the key for you. Here is how to add one to an object.

  1. Open the child object in Object Manager

    In Setup, go to Object Manager and select the object that will hold the foreign key, the child. The relationship field always lives on the child side, the many side of the relationship.

  2. Create a new field of type Lookup or Master-Detail

    Go to Fields and Relationships, click New, then choose Lookup Relationship for a soft key or Master-Detail Relationship for a hard key. Pick based on whether the child can exist without the parent.

  3. Choose the parent object

    Select the object the key points at. This becomes the related-to object. For a polymorphic standard field you cannot create this yourself; those ship with the platform on objects like Task and Event.

  4. Set the delete behavior and field-level security

    For a Lookup, choose what happens when the parent is deleted: clear the field, block the delete, or cascade if allowed. Set field-level security and add the field to the page layouts.

  5. Save and load data with the IDs populated

    Save the field. When you import records, populate the relationship column with the parent's record ID, or use an External ID on the parent and load with upsert so the key fills automatically.

Relationship typeremember

Lookup for an optional, independent link; Master-Detail for a required link where the child inherits sharing and the parent supports roll-up summaries.

Parent delete behaviorremember

On a Lookup, choose Clear the field, Don't allow deletion, or Cascade delete (limited). Master-Detail always cascades to children.

Allow Reparentingremember

A Master-Detail option that lets the child move to a different parent after creation. Off by default, which locks the child to its original parent.

Child relationship nameremember

Sets the related list label and the SOQL relationship name used for parent-to-child subqueries from the parent object.

Gotchas
  • A detail object can have at most two Master-Detail relationships, so plan junction objects before you hit that ceiling.
  • Converting a Lookup to Master-Detail requires every child to have a parent populated first; orphan rows block the change.
  • The relationship field goes on the child object, not the parent. Adding it to the wrong object is a common first mistake.
  • Master-Detail makes the field required and the child inherit sharing, which can surprise teams expecting the looser Lookup behavior.

See the full Foreign Key entry

Foreign Key includes the definition, worked example, deep dive, related terms, and a quiz.