Object Relationships in Salesforce
Let’s understand Object Relationships in salesforce using two tables.
We have “Teacher” and “Student” tables through which we are going to understand Primary and foreign key concepts to implement Relationships in salesforce. Every table has some unique identifier field, that field is called Primary Key, where as Foreign key is Primary key of some other related table.
In above given tables, Teacher Table has unique key field “Teacher Id” which is used in student Table as “foreign Key”.Just by looking at the table, we can understand that, one teacher is connected with many students or we can say multiple students connected with one teacher.
Now let’s say that Teacher is a parent table and students is a child table because they have one to Many Relationships between them. Here Teacher is at “one” side where Student is at “Many” sides.
In one to many relationships Primary key which is in parent table, need to be referred in child table as foreign key.
Now in the language of salesforce, the foreign key is the key to implement two relationships.
1) Lookup Relationship
2) Master Details relationship
We can classify salesforce object relationship as below.
1) One to Many — Lookup Relationship and Master Details Relationship
2) Many to Many — Junction Object Need to implement
A. Lookup Relationship:
In Lookup relationship, when we delete parent record or master record, all associated records are not deleted. For example, if a company wants to close Department where multiple employees are working but wants to retain all working employees of that department to shift them in other department. In this scenario we can go for Lookup Relationship. In above given tables, T1 Teacher has three students(S1,S2,S4) connected with him. And T2 Teacher has 2 students(S3,S5) connected with him. If we want to delete T1 record and retain all its associated records, in this case we have to implement Lookup Relationship.
We can have up to 40 lookup relationships per object due to governor limit. Salesforce support multi-tenant architecture in which resources are used on sharing basis. So that Salesforce has set some governor limits to limit the uses and to avoid wastage of resources.
Lookup Relationship is also called loosely coupled because parent does not control the behaviour of child.
In Lookup relationship, parent field on child object is optional.
We can not implement Rollup summary field on parent object. Rollup summary field is useful to find out Maximum, Minimum, count and sum.
Last but not least about Lookup relationship is that, ownership of child record is independent,
B. Master Detail Relationship:
Master Detail Relationship is also called as tightly coupled relationship because when parent record or master record is deleted all its associated child records are deleted. In Master-details Relationship, master object controls the behaviour of child or detail object. Ex. In above two tables, if we deleted T1 record from Teacher object or Teacher Table, all its associated record S1,S2,S4 are deleted from Student child.
Maximum 2 Master Details per object are allowed due to governor limits.
We can implement Rollup summary field such as Sum,Min,Max. We can implement up to 40 rollup summary fields and it can be increased.
Parent field on child is mandatory.
In master Detail relationship child object record inherits security settings from master.
C. Many-to-Many Relationship:
M-to-M relationship is implemented using junction object.Suppose we have Passenger object which is associated with train object. here in example one Passenger travels via many trains and One train can have many Passengers as well. For this scenario we have to create third object called” ticket” to implement Many to Many Relationship between passenger and trains.
D. Hierarchical Relationship:
This is special kind of Lookup Relationship available only the user object. When we want to lookup something from table itself.
Suppose we have two employees namely Mr.John and Mr.Addem in a company, Mr.John is working as Sales Representative and Mr.Addem is working as Sales Manager and Mr.John is reporting to Mr.Addem. In this scenario, we want to create Hierarchical lookup field on user objects so that we can select Mr.Addem as reporting manager of Mr.John from user List.
We can Convert Lookup Relationship to Master Detail Relationship if the lookup field in all records contain value.
When we convert lookup to master detail relationship then it changes the organization-wide default to Controlled by Parent and the sharing model is updated to public read/write.
We can also convert Master detail to lookup if no rollup summary fields exist on the master object.