Salesforce: Lookup vs Master-Detail

In Salesforce, relationships define how objects relate to each other. Today, let’s explore two important relationship types: Lookup and Master-Detail. Both are commonly used, but they serve different purposes and have unique features that distinguish them.
Lookup Relationship: College and Students
A Lookup Relationship is a one-to-one or one-to-many relationship where one object "looks up" to another object. In this relationship, the child record can exist independently of the parent. This means the parent record is not required for the child record to exist, and deleting the parent does not automatically delete the child.
Although commonly introduced as one-to-one, Lookup relationships in Salesforce are actually one-to-many from the parent to the child, just like Master-Detail. The key difference lies in the dependency and behavior, not in the cardinality.
Example: College and Students
Imagine a college that has many students. Each student is associated with a particular college, but a student can exist without being associated with a college (though that wouldn’t make much sense in the real world!). In this case, the relationship between the College (parent) and Student (child) is a Lookup Relationship.
College (Parent): College objects store information about colleges, such as their name, location, and courses offered.
Student (Child): Student records store details about students, such as their name, grade, and course selection. A student can be associated with a college via the lookup field, but even if a college record is deleted, the student records remain intact.
This is the essence of a Lookup Relationship: the child can exist without the parent, and deleting the parent doesn’t impact the child.
Master-Detail Relationship: Student and Ratings in Various Areas
A Master-Detail Relationship, on the other hand, is much more tightly coupled. In this relationship, the child record cannot exist without a parent. The child’s lifecycle is directly dependent on the parent’s lifecycle, meaning if you delete the parent, the child will also be deleted. Additionally, the child’s record cannot exist without being associated with the parent.
Example: Student and Ratings
Let’s take an example where a student’s ratings are tracked across various fields such as Labs, Exams, Games, and Extra-curricular activities. The Student (parent) record is mandatory for each of these ratings (child) records to exist.
Student (Parent): The student record includes information like name, age, and course enrolled.
Ratings (Child): The ratings of the student across different areas (labs, exams, sports, etc.) are stored as child records in a master-detail relationship. Each rating is tightly coupled with the student record. If the student record is deleted, all the associated ratings records are also deleted.
In this case, the ratings records will not exist without a student record. This illustrates the Master-Detail Relationship where the child’s existence depends entirely on the parent.
Some Common Questions and Answers on Salesforce Relationships
To add more value to your understanding, let’s dive into some common interview questions related to relationships in Salesforce:
1. What is the relationship between Account and Contact?
Answer: The relationship between Account and Contact is neither a Master-Detail nor a Lookup relationship. It’s a special relationship that includes cascade delete. Here’s how it works:
Cascade Delete: When an Account is deleted, all associated Contact records are also deleted.
To check: If you try creating a Contact record, the Account field is not mandatory. This means it’s not a Master-Detail relationship. However, if you delete the associated Account, the Contact record is also deleted, indicating it’s a special type of relationship with cascade delete.
Limitation: You cannot create a Rollup Summary field on Account for Contact.
2. What is the relationship between Account and Opportunity?
Answer: While it might seem like a Master-Detail relationship, Account and Opportunity have some exceptions.
Even though Opportunities are typically linked to an Account, you can create an Opportunity without filling in the Account field, which may make you think it’s a Lookup relationship. However, Salesforce treats this as a Master-Detail relationship with exceptions.
This is an example of how Salesforce sometimes implements exceptions to standard relationships.
3. Can we create a master-detail relationship with a standard object as a child?
Answer: No, standard objects can never be on the child side of a Master-Detail relationship.
4. Can we convert a Lookup relationship to Master-Detail?
Answer: Yes, it’s possible to convert a Lookup relationship to Master-Detail, but there are a few key considerations:
Every child record must have a parent record. Before converting, ensure that all child records have a populated Lookup field.
If there are no child records, you can directly convert the relationship.
The Master-Detail relationship requires that child records always have a parent, so you must ensure that no child record is orphaned.
5. Can we convert Master-Detail to Lookup?
Answer: Yes, it is possible to convert a Master-Detail relationship to a Lookup relationship. However, before doing this, you must:
Delete any Rollup Summary fields on the parent object.
Ensure that the relationship between the parent and child is no longer as tightly coupled as it was in the Master-Detail relationship.
Subscribe to my newsletter
Read articles from Akshay Sakinam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
