An object relationship in Salesforce can be defined as a two-way link or association between two or more objects. Relationships are created by generating custom relationship fields on an object. Due to this, whenever a user views the records, they are also able to see and access related data. The various types of object relationships in Salesforce are:
Master-Detail relationship (1:n):
- It is a parent-child relationship where the master object controls the behavior of dependent detail and sub-detail object. It is a 1:n relationship, in which there is only one parent, but multiple children.
- The master field cannot be empty, since it is the controlling object. If a master object’s record or field gets deleted, the corresponding record or fields in the dependent object will also be deleted. This is called a cascade delete. Child object fields will inherit the owner, sharing, and security settings from the parent object. You can create Master-Detail relationships between two custom objects, or between a standard object and custom object until the standard object remains as a master in the relationship.
- For example, you can define a Master-Detail relationship with two objects such as Account—Expense report. It expands the relationship to sub detail records, such as Account—Expense report—Expense line item. You will be then allowed to perform operations across the master—detail—sub detail relationship.
Lookup relationship (1:n):
- These relationships are useful for creating a link between two objects, without the dependency on the parent object. This is again a form of parent-child relationship where there is only a single parent, but multiple children exist (1:n relationship).
- Despite master being the controlling field, deleting a master record will not automatically delete the lookup field in the child object. So the records in the child object will not get affected and there will not be any cascade delete here. Child fields will not inherit the owner, sharing, or security settings of its parent.
- With a lookup relationship, you are permitted to link two different objects or link an object with itself (except for the user object). For example, you may want to link a custom object “Bug” with itself for showing how two different bugs are related to the same problem.
Junction relationship (Many-To-Many):
- You can use this kind of relationship whenever you want to create two Master-Detail relationships. By linking 3 custom objects, we can create two Master-Detail relationships. In this, two objects will be represented as master objects and the third object will act as a dependent on both of them. In simpler words, we can say that there will be a child object for both master objects.
- This relationship allows every record of one object to be linked with several records of another object and vice versa. For example, you can create a custom object “Bug” that relates to a standard object in such a way that a bug can be related to multiple causes, and also a single case can be related to multiple bugs.
- This is a special lookup relationship, that is available only for the user object.
- It allows users to use a lookup field to associate a single user with another user that does not refer to itself in a direct or indirect way. For example, to store the direct manager of each user, you are allowed to create a custom hierarchical relationship field.
External lookup relationship:
- This relationship links a child’s standard, custom, or external object to a master external object.
- In this relationship, the standard External ID field values on the parent external object are matched against the external lookup relationship field values of the child object.
Indirect lookup relationship:
- This relationship links a child’s external object to a master standard or custom object.
- For matching and associating the records, you will mention the parent object field and the child object field when you create an indirect lookup relationship field on an external object. Here, you have to select the parent object’s custom unique, external ID field to match against the child object’s indirect lookup relationship field.