You cannot create a new Master-Detail relationship on an existing custom object if records already exist

While Creating Master Detail relationship between two object an error will be displayed like“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”

When we try to create Master Detail relationship usually the above error will not be displayed before creating any records in Child Object. After creating records in Child objects then only Master Detail relationships can not be established between parent Object and Child Object.

In Master Detail relationship, parent object does not allow orphaned child records which means every record in the child object must be associated to parent Object.

Example :- Creating Master Detail Relationship Between Hospital (Parent Object)and Invoice Object(Child Object).

Here we have already created some records in Invoice Object. Now we have to create Lookup relationship between master object and detail object. we can lookup relationship because it is loose bond relationship and it does not require every child record need not be associated to a parent.

  1. Create Lookup Relationship between Hospital and Invoice Object.
  2. Edit all Invoice records and enter Hospital object(Parent Object field).
  3. Now Change lookup relationship to Master Detail relationship.

Here we have already created some records in Child Object let us try to create Master Detail relationship between Invoice Object and Hospital Object.

Go to Setup | Build | Create | Object | Select Child Object (Invoice) | Fields and Relationships | New 

Click on Master Detail relationship and click on next button.

Enter Related object as Hospital. Click on Next button.

You cannot create a new Master-Detail relationship on an existing custom object if records already exist

As shown above, we can not You cannot create a new Master-Detail relationship on an existing custom object if records already exist. We must create Lookup relationship between the two object.

Creating Lookup Relationship in Salesforce.

  • Click on view field and create new lookup relationship field.

We have successfully created Lookup relationship field in Invoice Object.

We have created Lookup relationship field in Invoice Object.

  • Now go to Invoice Object
  • Click on Invoices as shown above.
  • Now the list of records that are present in Invoice Object will be displayed. Now click on any record and edit the record as shown below.

We have created Lookup relationship field in Invoice object but in Invoice record the master object field is empty. Now we have to populate all child records with a valid lookup field to the parent.

  • Now double click on the Hospital field as shown below.
  • Click on Lookup button and select Hospital as shown below.
  • Like wise Edit all Child record and populate all the records with lookup value.
  • Now click on Save button.
  • As shown above we have successfully populated value for INVOICE – 001.

Changing Lookup Relationship to Master Detail Relationship.

Go to Setup | Build | Create | Object | Child Object (Invoice) | Hospital | Edit.

  • Now click on Change Field Type button as shown below.

  • Select data type as Master Detail Relationship as shown below.

  • And Finally Click on Save button as shown below.

Successfully we have created Master-Detail relationship in Salesforce as shown below.

Conclusion

We have practically learnt how to create Master detail Relationship between two objects when an error like “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” occurs.