Relationship in Salesforce refers to the way two or more objects are connected or related to each other within the data model. Relationships define how records in one object are associated with records in another object, allowing us to establish connections and retrieve related data efficiently. Salesforce supports different types of relationships between objects, each serving a specific purpose and offering different functionalities.
Types of relationship in Salesforce
Mainly we have below five types of relationship in Salesforce. These will handle connection between objects for different business use case.
- Lookup Relationship
- Master-Detail Relationship
- Many-to-Many Relationship
- External Lookup Relationship
- Self-Relationship or Hierarchical Relationship
1. Lookup Relationships
A lookup relationship is a basic and non-hierarchical association between two objects. In this relationship, one object (the child) contains a field that holds a reference (lookup) to a record in another object (the parent). It enables us to link records between objects but does not enforce data integrity or automatic deletion of child records when the parent record is deleted.
Key characteristics of a lookup relationship include:
- Unidirectional Relationship: Lookup relationships are unidirectional, meaning that we can establish a reference from the child object to the parent object, but not vice versa. The parent object does not have any direct knowledge of the records in the child object.
- One-to-Many Relationship: Each record in the child object can have a reference to only one record in the parent object. However, multiple child records can point to the same parent record, creating a one-to-many relationship.
- Optional Relationship: Lookup relationships are optional, which means that the lookup field in the child object can be left blank (null) if there is no associated parent record.
- No Cascading Deletion: Deleting a parent record does not automatically delete the child records. The child records will still exist, but their lookup field will no longer reference the deleted parent record.
- Data Integrity: Unlike master-detail relationships, lookup relationships do not enforce data integrity constraints. This means that we can create orphaned child records (i.e., child records without a parent) without any error or restriction.
- Use Cases: Lookup relationships are commonly used when we want to create relationships between standard or custom objects, but we do not require the strong enforcement of data integrity or cascading deletion provided by master-detail relationships
To create a lookup relationship in Salesforce, we typically create a custom field with the “Lookup” data type on the child object, which allows us to select the parent object and define the relationship. Lookup relationships play a vital role in connecting data between different objects and are widely used in Salesforce data modelling to build complex and flexible data structures.
2. Master-Detail Relationship
A master-detail relationship is a stronger type of relationship between two objects. In this relationship, the child object is referred to as the “detail,” and the parent object is referred to as the “master.” The master-detail relationship links the child records to a specific parent record and allows us to set behaviours such as record ownership, security settings, and the automatic deletion of child records when the parent record is deleted.
key characteristics and benefits of master-detail relationships in Salesforce:
- Data Ownership and Access Control: In a master-detail relationship, the master object controls the ownership and access settings for the detail records. The ownership of the detail records is transferred to the owner of the master record. This allows for centralized control over data access and sharing rules.
- Cascade Deletion: With a master-detail relationship, we can define the behaviour for cascade deletion. When a master record is deleted, all associated detail records are automatically deleted. This ensures data integrity and helps maintain data consistency.
- Roll-Up Summary Fields: Master-detail relationships enable the use of roll-up summary fields. These fields allow us to perform calculations and aggregate data from related detail records onto the master record. This simplifies data analysis and reporting by providing summarized information at the master record level.
- Validation and Workflow Rules: Master-detail relationships allow us to define validation rules and workflow rules that operate across both the master and detail records. This helps maintain data accuracy and enforce business processes consistently.
- Security and Sharing: Master-detail relationships inherit the sharing settings and security settings from the parent object to the child object. This ensures that the same level of security and sharing rules apply to both the master and detail records.
- Parental Access to Child Records: With a master-detail relationship, the parent object has full access to all related child records. This facilitates data visibility and collaboration between related records.
- One-to-Many Relationship: Each detail record is associated with a single master record, creating a one-to-many relationship. This means that multiple detail records can be linked to the same master record.
To establish a master-detail relationship in Salesforce, we define a custom field with the “Master-Detail” data type on the detail object, referencing the master object. The creation and management of master-detail relationships require careful consideration of data relationships, access controls, and data modelling best practices.
3. Many-to-Many Relationship
In Salesforce, a many-to-many relationship refers to a scenario where multiple records from one object can be associated with multiple records from another object, and vice versa. Salesforce doesn’t support native many-to-many relationships between objects. However, we can emulate a many-to-many relationship by using a junction object. A junction object is a custom object that connects two or more objects through lookup relationships.
Here’s how we can create a many-to-many relationship using a junction object:
- Define the Objects: Let’s say we have two objects that we want to establish a many-to-many relationship between. For example, consider two objects: “Product” and “Opportunity.”
- Create the Junction Object: Create a new custom object, let’s call it “Opportunity Product,” to act as the junction object. The junction object will have two lookup fields: one for “Product” and another for “Opportunity.”
- Setup the Lookups: On the “Opportunity Product” object, create two lookup fields: one that looks up to the “Product” object and another that looks up to the “Opportunity” object.
- Establish Relationships: Create lookup relationships between “Opportunity Product” and “Product” as well as between “Opportunity Product” and “Opportunity.” This allows us to connect multiple products to one opportunity and vice versa.
- Add Records: When we need to associate a product with an opportunity, we create a new “Opportunity Product” record and set the lookup fields to the appropriate product and opportunity records.
By using the junction object, we can effectively create a many-to-many relationship between “Product” and “Opportunity.” The “Opportunity Product” records act as bridges that link multiple products to multiple opportunities, enabling us to capture the many-to-many relationship.
Junction objects are versatile and can be used to represent various types of many-to-many relationships in Salesforce. They are commonly employed in scenarios like associating contacts with multiple accounts, connecting students with multiple courses, or linking participants to multiple events.
4. External Lookup Relationship
An external lookup relationship allows us to establish a relationship between a Salesforce object and an external object stored outside of Salesforce. This type of relationship is useful when we need to link Salesforce records with data in an external system.
Key points about External Lookup Relationships in Salesforce
- Connecting to External Systems: With an External Lookup Relationship, we can connect Salesforce objects with data stored in external systems such as databases, ERP systems, or other cloud platforms. This enables us to access and reference external data directly within Salesforce.
- Data Synchronization: An External Lookup Relationship facilitates data synchronization between Salesforce and the external system. We can map fields between Salesforce and the external system to ensure data consistency and accuracy across both platforms.
- Field Mapping: We define field mappings between the Salesforce object and the external object to establish the relationship. This allows data to be queried, updated, and displayed from both systems.
- Data Visibility: Through the External Lookup Relationship, we can access and display data from the external system within Salesforce. This enhances the user experience by providing a unified view of information from both Salesforce and the external system.
- Cross-Platform Reporting and Analytics: External Lookup Relationships enable us to create reports and dashboards that combine data from Salesforce and the external system. This allows for comprehensive analysis and reporting across integrated data sources.
- Integration Options: Salesforce offers various integration mechanisms to establish External Lookup Relationships, including Salesforce Connect, which supports real-time or near real-time data integration, as well as custom integration approaches using APIs and middleware solutions.
By leveraging External Lookup Relationships, organizations can leverage data from external systems within Salesforce, providing a more comprehensive and integrated view of their business processes and data landscape. It enables seamless integration and synchronization, empowering users to access and work with data from multiple sources within the familiar Salesforce interface.
5. Self-Relationship or Hierarchical Relationship
A self-relationship is a relationship between records of the same object. It allows us to establish connections between records within the same object, often used to represent hierarchical or related data structures.
Key points about Self-Relationships in Salesforce:
- Connecting Records within the Same Object: A self-relationship allows us to establish relationships between records within the same Salesforce object. For example, in a custom object called “Employee,” we can create a self-relationship to connect an employee record with another employee record.
- Hierarchical Structures: Self-relationships are often used to represent hierarchical structures within an object. For instance, we can create a self-relationship in an object called “Account” to establish a hierarchical relationship between parent accounts and child accounts.
- Parent-Child Relationships: With a self-relationship, we can define parent-child relationships within the same object. This enables us to create multi-level hierarchies or nested structures. For example, an employee record can have a parent employee and multiple child employees.
- Lookup Fields: Self-relationships use lookup fields to establish the connections between records. The lookup field within the object points to another record within the same object.
- Record Navigation and Reporting: Self-relationships allow for easy navigation between related records within the same object. We can traverse the hierarchy or relationships to access parent records, child records, or siblings. Additionally, we can use self-relationships in reporting to generate insights or summaries based on hierarchical or related data.
- Data Integrity and Validation: Salesforce provides features such as validation rules, triggers, and workflows that can be applied to self-relationships to ensure data integrity and enforce business rules. For example, we can define validation rules to prevent circular references or enforce specific conditions within the self-relationship.
Self-relationships provide a flexible way to represent hierarchical or related data structures within a single Salesforce object. They enable navigation, reporting, and enforcement of business rules within the context of the object itself. By utilizing self-relationships, we can organize and manage data in a way that reflects the inherent relationships and dependencies within our business processes.
Relationships in Salesforce help create meaningful connections and enable efficient data management. They allow us to access related data, navigate between objects, enforce data integrity, and establish hierarchical or many-to-many associations. Understanding and utilizing relationships effectively is crucial for designing robust and scalable data models in Salesforce.