How to create a Master-Detail relationship in Salesforce

A Master-Detail relationship in Salesforce creates a tightly connected parent-child data model. The master record is the parent, and the detail record is the child. A detail record cannot exist without a master record, and when the master record is deleted, Salesforce also deletes the related detail records.

This tutorial explains when to use a master-detail relationship, where to create it, how it differs from a lookup relationship, and how to create one between Pharma Product and Sample. The screenshots below use the existing setup screens from this tutorial; in Lightning Experience, you usually start from Setup > Object Manager, but the relationship choices are the same.

For related basics, read the TutorialKart guide on Salesforce object relationships. Salesforce also documents relationship behavior in its official Object Relationships Overview and Trailhead module on Object Relationships.

What a Salesforce Master-Detail relationship does to child records

In a master-detail relationship, the child object is dependent on the parent object. Salesforce uses this relationship when child records must belong to exactly one parent and should follow the parent record’s access and lifecycle.

  • The relationship field is created on the detail object, not on the master object.
  • The detail record must always be associated with a master record.
  • Deleting a master record also deletes its related detail records.
  • The detail record’s record access is controlled by the master record.
  • Roll-up Summary fields can be created on the master object to summarize detail records.
  • In normal custom setup, a custom object can be the detail object. A standard object such as Account can be used as the master, but it cannot be made the detail object by adding a custom master-detail field.

Example: Account is a standard object, so you do not create a custom master-detail field on Account to make Account the detail object. However, a custom object can have Account as its master object when Salesforce allows that relationship for the selected objects.

Master-Detail relationship vs Lookup relationship in Salesforce

The main difference between a master-detail relationship and a Lookup relationship is dependency. A lookup relationship is more flexible, while a master-detail relationship is stricter and is used when the child record should depend on the parent.

Salesforce relationship behaviorMaster-Detail relationshipLookup relationship
Child record without parentNot allowedCan be allowed depending on field settings
Record accessControlled by the parent recordUsually independent from the parent record
Parent record deletionDeletes related detail recordsDoes not always delete related records by default
Roll-up summary fieldsSupported on the master objectNot supported by standard roll-up summary fields
Best use caseLine items, samples, invoice items, or other dependent recordsLoose references, optional associations, or related records that can stand alone

Before creating a Master-Detail relationship in Salesforce

Check these points before you create the field. This avoids common errors during setup and helps you choose the correct relationship type.

  • Confirm which object is the master and which object is the detail.
  • Create the relationship field on the detail object. For example, create the field on Sample when Pharma Product is the parent.
  • Use a master-detail relationship only when every detail record must have a parent.
  • Check whether the detail object already has the maximum number of master-detail relationships. Salesforce allows a maximum of two master-detail relationships on an object.
  • Review sharing requirements because detail record access is inherited from the master record.
  • Plan roll-up summary fields on the master object if you need counts, sums, minimum values, or maximum values from the detail records.

Master-Detail relationship error when child records already exist

When creating a Master-Detail relationship in Salesforce, you may see an error similar to this: “You cannot create a new Master-Detail relationship on an existing custom object if records already exist. You must first create a Lookup relationship, populate the lookup field with data in all records, and then change the relationship type to Master-Detail.”

This happens because master-detail does not allow orphaned child records. If the child object already contains records, first create a lookup relationship, populate that lookup field for every existing child record, verify that no child record is missing a parent, and then convert the lookup relationship to a master-detail relationship where Salesforce permits the conversion.

Create the Master-Detail relationship field on the Salesforce child object

In this example, we create a one-to-many relationship between Pharma Product and Samples. Pharma Product is the master object, and Sample is the detail object. Each Sample record must be connected to one Pharma Product record.

  • Go to Setup | Build | Create | Objects.
Master Detail relationship in Salesforce

When you click Object, Salesforce displays the list of custom objects. Click the child object on which you want to create the Master-Detail relationship. In this example, the child object is Sample.

Master-Detail relationship in Salesforce

Now navigate to the Sample object and open it. The object definition page is displayed. Go to Custom Fields and Relationships.

Master-Detail relationship in Salesforce

Click New to create a new custom field and relationship field on the Sample object.

Step 1: Choose Master-Detail Relationship as the field type

In the field type list, select Master-Detail Relationship. This creates a required relationship field on the detail object.

Master-Detail relationship in Salesforce

Click Next and choose the related object in the next step.

Step 2: Choose the Salesforce master object as the related object

The related object is the parent or master object. For this tutorial, choose Pharma Product as the related object.

Master-Detail relationship in Salesforce

Select Pharma Product as the object related to Sample. Click Next.

Master Detail relationship in Salesforce

Enter or review the field label, field name, and relationship name. Use names that clearly describe the parent record. Then click Next.

Master-Detail relationship in Salesforce

Set field-level security for the profiles that should see the relationship field. Because the field is required for detail records, review visibility carefully. Click Next.

Master-Detail relationship in Salesforce
Master-Detail relationship in Salesforce

Add the relationship field to the required page layouts and decide whether the related list should appear on the master object’s page layout. Click Next.

Creating the Master-Detail relationship between Pharma Product and Sample is now ready to be saved. Click Save to finish the setup.

Master Detail relationship in Salesforce

Test the Salesforce Master-Detail relationship with a Sample record

After creating the relationship field, test it by creating a detail record. Go to the child object, Samples, and create a new Sample record.

Master-Detail relationship in Salesforce

If there are no Sample records, click New to create the first child record.

Master-Detail relationship in Salesforce

Enter the required Sample details such as Quantity.

Master-Detail relationship in Salesforce

The master-detail field requires you to select one Pharma Product for the Sample. This confirms that the relationship is on the child object. Here, Sample is the detail object, and Pharma Product is the master object. Select the Pharma Product record and click Save.

Master-Detail relationship in Salesforce

Now create two more Sample records and associate them with the same Pharma Product record. This makes it easier to see the related list on the parent record.

  • Now click on Parent object(Pharma Object).
Master-Detail relationship in Salesforce
  • Go to vitamin D.
Master-Detail relationship in Salesforce

After opening the parent Pharma Product record, you can see the related list for Samples. This related list appears because the master-detail relationship connects one Pharma Product record to many Sample records.

Master Detail relationship in Sfdc

Here, the Pharma Product record Vitamin D is associated with multiple Sample records. This is the parent-child structure created by a Master-Detail relationship in Salesforce. A child Sample record must have a parent Pharma Product record; it cannot be saved as an orphan record.

Salesforce Master-Detail relationship checklist after setup

Use this checklist to review the relationship after saving it.

  • Create a new detail record and verify that the master field is required.
  • Open the master record and confirm that the child related list appears on the page layout.
  • Check whether users can see and edit the detail records through the parent record’s access settings.
  • Create a roll-up summary field on the master object if the business needs a count, sum, minimum, or maximum from child records.
  • Test deletion behavior in a sandbox before using the relationship in production data.

Frequently asked questions about Master-Detail relationship in Salesforce

Where do you create a Master-Detail relationship in Salesforce: on the child object or parent object?

You create the Master-Detail relationship field on the child object, also called the detail object. In this tutorial, the relationship field is created on Sample because Sample is the child object, and Pharma Product is the parent object.

What is the difference between a Lookup relationship and a Master-Detail relationship in Salesforce?

A Lookup relationship is a looser association and can allow records to exist independently, depending on field settings. A Master-Detail relationship is stricter: the detail record must have a master record, access is controlled by the master, and deleting the master deletes the related detail records.

How many Master-Detail relationships can we create on one Salesforce object?

Salesforce allows a maximum of two Master-Detail relationships on an object. If your data model needs more parent references, review whether some relationships should be lookup relationships instead.

Can a Salesforce detail record exist without a master record?

No. In a Master-Detail relationship, the detail record cannot be saved without selecting a master record. This is why existing child records must be populated with parent values before converting a lookup relationship to master-detail.

Can we create a roll-up summary field for a Master-Detail relationship?

Yes. Roll-up Summary fields are created on the master object and summarize records from the detail object. Common examples include counting child records or summing a numeric field from child records.

Master-Detail relationship recap for Salesforce admins

In this Salesforce tutorial, we created a Master-Detail relationship in Salesforce by adding the relationship field on the child object, choosing the parent object, saving the field, and testing the related list on the parent record. Use this relationship when the child record must depend on the parent record. If the child record should be optional or independently owned, use a lookup relationship instead. Next, learn how to create Lookup relationship in Salesforce.