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.
- 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.
- 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.
- 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.
- 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.
- 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.
Lookup for an optional, independent link; Master-Detail for a required link where the child inherits sharing and the parent supports roll-up summaries.
On a Lookup, choose Clear the field, Don't allow deletion, or Cascade delete (limited). Master-Detail always cascades to children.
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.
Sets the related list label and the SOQL relationship name used for parent-to-child subqueries from the parent object.
- 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.