Hello all, as the name suggests, in this blog, you will get to know about relationships between objects in Salesforce, what types of relationships are there in Salesforce and how they behave.
In terms of Salesforce, relationships tell us how two or more objects relate with each other or it is a bi-directional association between objects. Suppose we have to relate two objects in Salesforce so that their functionality depends upon each other, then we use relationships to create a link between them. Salesforce platform allows the following relationships:
Don’t forget to check out: How to Create Relationship between fields in Salesforce using Look up and Master Detail
- Master-Detail relationship
- Look Up relationship
- Many-to-Many relationship
- External relationship
1. Master-Detail relationship:
In this type of relationship, one object acts as a master and other act as a detail object. In other words, this relationship acts as a parent-child relationship. We use this type of relationship when we want to bound two objects tightly or closely dependent on each other.
The behavior of Master-Detail relationship:
- When a master record gets deleted, its related child/detail record automatically gets deleted.
- In a master-detail relationship both the objects are strongly coupled to each other.
- Sharing and security settings of the detail records are inherited as per their master settings.
- We can define this relationship in between custom objects and in between standard and custom objects. Note that, the standard object must be on the master side while creating relationships between standard and custom objects.
- We can also have roll-up summary fields in master-detail relationships which helps us to calculate values from child records by using aggregate functions like Count, Sum, Min, Max.
- The master-detail field is required on the detail record’s page layout.
- We can only create a maximum of 2 master-detail relationships per object. In a master-detail relationship both the objects are strongly coupled to each other.
2. Look Up relationship:
In this, both objects are loosely coupled, which means that if one gets deleted then the related object will not get deleted.
The behavior of the Look-Up relationship:
- In this, objects are loosely coupled.
- When a parent's record gets deleted, the child remains in existence.
- We cannot create a roll-up summary field in a lookup relationship.
- Parent and child records have their own sharing and security settings in look-up relationships.
- The Look-Up relationship field is not mandatory by default but we can select a checkbox to make it mandatory in lightning.
- We can have a maximum of 40 look-ups per object.
Some important points to remember:
- If we delete a child record and then restore it from the recycle bin, the master-detail relationship gets lost at the time of deletion and does not get restored after child/detail getting restored.
- To convert a master-detail to look-up we have to check that there is no roll-up summary field available and can convert a look-up to master-detail if lookup field in all records contains a value.
3. Many-to-Many relationships:
In many-to-many relationships, records of particular objects are linked to multiple records of different objects and vice versa. There is no such field as a many-to-many relationship in Salesforce, we can create a many-to-many relationship by creating two master-detail relationships with a common object. This common object can also be specified as the junction object.
Self-relationship simply means creating a relationship with itself. In this, we can relate an object with itself by look-up. Example: Account object has a field called Parent Account which shows the self-relationship in Account.
5. External relationship:
This is a new field type that has been introduced with Salesforce Connect. To link an external object to another external object, we use the external relationship field. It supports standard look-up relationships that use 18 characters Salesforce Id for the association.