Relationship types
So, what is a relationship exactly? A relationship is a bi-directional link between two objects. Without relationships, you could create as many standalone custom objects you would like to, but you would not have a way to link them to each other. By creating links between records, you can expose data about other related records on the page layout of a particular record.
While creating relationships between objects, you have to think in advance on how those objects will relate to each other. Therefore, there are three main relationship types:
- One-to-one relationship: This is a direct relation with another object's record and there can only be one at all times. For example, a husband will have only one wife at any given point in time. Historically, he could have been married multiple times, but at any given time, he only has one current wife.
- One-to-many relationship: This is a relationship where a certain record can have a relation with multiple records of the other object type. For example, one soccer team has multiple players in the team, but each player belongs only to one soccer team at any given time.
- Many-to-many relationship: In this case, each record of object A can have multiple records of object B, and object B can have multiple records of object A. Let's give an example from our movie database—a certain movie can have multiple people playing a role in the movie, and one person can play a role in multiple different movies! I think you can already see the problem here, right? To establish a many-to-many relationship, we will need another object between those two objects that acts as a junction, and such an object is called a junction object. So, in our example, we will create a specific object called Cast to add relationships between one movie and multiple people with a specific role in the movie, but it will automatically show different movies a person plays a role in.
The following diagram illustrates those relationships types:
In Salesforce, we create these types of relationships by using one or more relationship fields.
There are two specific types of relationship fields in Salesforce:
- Lookup: You use a lookup when there is no tight relationship, meaning that a record does not have to have a related record of the other type linked to it
- Master-detail: This is used when you define a tight relationship between two objects, meaning that the child cannot exist without a related parent
To relate two objects through a one-to-many relationship, you should always first create the relationship field on the child object (the many sides). Consider the following table:
There are other types of relationships in Salesforce too, but these are used less:
- Hierarchical: This is a special Lookup relationship that's only available on the User object. It lets users use a lookup field to associate a user with another user; for example, to determine someone's manager.
- External lookup: This links a child standard, custom, or external object to a parent external object, like when using Lightning Connect (this lets you access external data while using Salesforce) to make a connection to an external database source and making that data visible in Salesforce without storing it in Salesforce (this is less important for the exam).
- Indirect lookup: This links a child external object to a parent standard or custom object (this is less important for the exam, but it is good to know that it exists).
The special case here is the many-to-many relationship. A many-to-many relationship allows you to relate each individual record of an object to multiple records of another object and the other way around. To create such a relationship, both objects are linked together through a junction object. A junction object is a custom object that is the child of both objects through two master-detail relationships!