Relationships in Salesforce

Relationships in Salesforce

On January 20, 2022, Posted by , In Salesforce Admin, With Comments Off on Relationships in Salesforce
What are relationships in Salesforce? Types of Relationships in Salesforce
What are relationships in Salesforce? Types of Relationships in Salesforce

Table of Contents

Object Relationships are very important when you want to expand your data model and take it to the next level by defining a connection among objects. For example, if you have say custom objects named as “School” and “Student” then it is important to define a relation between these 2 objects to see to which school a specific student belongs to or number of students in a specific school. 

Therefore to implement such use cases, Salesforce provides special types of fields to define different types of relations.

What are Relationships in Salesforce?

In Salesforce, relationships refer to the connections between different data objects within the platform, enabling you to associate and link records in a structured manner. These relationships are crucial for organizing data effectively, allowing users to access related information seamlessly. By establishing these connections, Salesforce facilitates comprehensive data management, analysis, and reporting, making it easier to see the complete picture of your business operations.

Salesforce offers various types of relationships, such as master-detail and lookup relationships. Master-detail relationships are tightly coupled, where the master object controls certain behaviors of the detail object, including record ownership and security settings. Lookup relationships are more flexible, allowing you to link records from different objects without the same level of dependency. These relationships serve as the glue that holds your data together, ensuring a cohesive and efficient data management system.

Read the Latest Salesforce interview questions and answers.

Types of relationships 

Master-Detail Relationship

A Master-Detail relationship in Salesforce is a tightly coupled relationship where the master (parent) object controls the behavior of the detail (child) object. The detail record cannot exist without the master record. Key characteristics include ownership and sharing rules, cascade delete, and the ability to create roll-up summary fields on the master object to aggregate data from related detail records. This type of relationship is ideal for situations where the child’s lifecycle is dependent on the parent.

Read more about Master-detail relationships in Salesforce.

To create a Master-Detail relationship, you typically do this through Salesforce setup. Here’s an example of how you might define it in a custom object schema file.

<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>Invoice__c</fullName>
    <fields>
        <fullName>Account__c</fullName>
        <type>MasterDetail</type>
        <referenceTo>Account</referenceTo>
        <relationshipName>Invoices</relationshipName>
        <relationshipOrder>0</relationshipOrder>
    </fields>
</CustomObject>

Checkout: Variables in Salesforce Apex

Lookup Relationships

A Lookup relationship is a loosely coupled relationship where one object can reference another. Unlike Master-Detail, the child record in a Lookup relationship can exist independently of the parent record. Lookup relationships do not inherit sharing and security settings from the parent and allow for more flexibility. They are suitable for scenarios where the relationship between records is not critical to their existence.

Read more: Lookup relationships in Salesforce.

Creating a Lookup relationship can also be done via the Salesforce setup. Below is an example schema file entry for a Lookup relationship.

<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>Invoice__c</fullName>
    <fields>
        <fullName>Contact__c</fullName>
        <type>Lookup</type>
        <referenceTo>Contact</referenceTo>
        <relationshipName>Invoices</relationshipName>
        <relationshipOrder>0</relationshipOrder>
    </fields>
</CustomObject>

Watch our FREE Salesforce online course video, it’s a full length free tutorial for beginners.

Lookup RelationshipMaster-Detail Relationship
Loosely coupled relationship: Lookup relationships are more flexible and less dependent, allowing records to exist independently.Tightly coupled relationship: Master-detail relationships are more dependent, meaning child records rely heavily on the parent record.
Independent parent and child records: In a lookup relationship, parent and child records can exist independently of each other. Changes to one do not necessarily affect the other.Dependent child records: In a master-detail relationship, child records are dependent on the parent record, inheriting certain properties and behaviors from the parent.
No automatic deletion: When a parent record is deleted, child records in a lookup relationship remain intact and are not automatically deleted.Automatic deletion of child records: When a parent record is deleted, all its associated child records are also deleted in a master-detail relationship.
Independent ownership and sharing settings: Child records in a lookup relationship maintain their own ownership and sharing settings, which can be different from the parent record.Inherited ownership and sharing settings: Child records in a master-detail relationship inherit the ownership and sharing settings from the parent record, ensuring consistency and control.

Many-to-Many Relationship

A Many-to-Many relationship allows records of one object to be linked to multiple records of another object and vice versa. This is implemented using a junction object, which has two Master-Detail relationships with the objects being linked. This setup is useful for scenarios where there is a need to associate multiple records from one object with multiple records from another object, such as associating students with courses.

A Many-to-Many relationship involves creating a junction object. Below is an example of how to define such relationships in schema files.

Junction Object (CourseEnrollment__c)

<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>CourseEnrollment__c</fullName>
    <fields>
        <fullName>Student__c</fullName>
        <type>MasterDetail</type>
        <referenceTo>Student__c</referenceTo>
        <relationshipName>Enrollments</relationshipName>
        <relationshipOrder>0</relationshipOrder>
    </fields>
    <fields>
        <fullName>Course__c</fullName>
        <type>MasterDetail</type>
        <referenceTo>Course__c</referenceTo>
        <relationshipName>Enrollments</relationshipName>
        <relationshipOrder>0</relationshipOrder>
    </fields>
</CustomObject>

Checkout: Data types in Salesforce Apex

External Lookup Relationship

An External Lookup relationship is used to link a child object to a parent object that resides outside of Salesforce. This is particularly useful for integrating with external data sources. The external lookup relationship allows Salesforce to reference external data directly, ensuring that the system can operate seamlessly with external databases or applications.

For an External Lookup relationship, you define it in the schema file of the custom object. Here’s an example.

<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>Order__x</fullName>
    <fields>
        <fullName>Customer__c</fullName>
        <type>ExternalLookup</type>
        <referenceTo>Customer__x</referenceTo>
        <relationshipName>Orders</relationshipName>
        <relationshipOrder>0</relationshipOrder>
    </fields>
</CustomObject>

Readmore: Validation Rules in Salesforce

Indirect Lookup

An Indirect Lookup relationship is used to link an external object to a standard or custom object within Salesforce based on a custom field mapping. This type of relationship is useful when you need to relate data stored outside Salesforce with data stored inside Salesforce, allowing for efficient integration and referencing without duplicating data.

An Indirect Lookup relationship involves mapping a custom field to an external object field. Below is an example schema file entry.

<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>Order__x</fullName>
    <fields>
        <fullName>CustomerId__c</fullName>
        <type>IndirectLookup</type>
        <externalLookupRelationshipName>Customer__x</externalLookupRelationshipName>
        <lookupReferenceTo>Account</lookupReferenceTo>
    </fields>
</CustomObject>

Readmore: Custom Page Layouts in Salesforce

Hierarchical Relationships

A Hierarchical relationship is a special type of Lookup relationship available only for the User object. It allows users to create a hierarchy within the User object to model relationships between users, such as managers and subordinates. This relationship type is useful for creating organizational structures and can be leveraged in approval processes and reporting hierarchies.

A Hierarchical relationship is specific to the User object and can be set up in the schema file as follows.

<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>User</fullName>
    <fields>
        <fullName>Manager__c</fullName>
        <type>Hierarchy</type>
        <relationshipName>Subordinates</relationshipName>
        <relationshipOrder>0</relationshipOrder>
    </fields>
</CustomObject>

Comparison of Lookup and Master-Detail Relationships

Lookup RelationshipMaster-detail Relationship
Limit – We can create a maximum of 40 lookups on an object.Limit – We can only create maximum of 2 master details on an object. Salesforce support can be contacted to increase the limit
A child record can exist without a parent.A child cannot exist without a parent 
By default record possession of child records isn’t controlled by the parent.Record ownership of child records is controlled by the parent
Cancellation of the parent fair erases the related field within the child but not the full recordOn the off chance that a ace record is erased at that point the record of its detail too gets erased beside it.
While creating a child record, a parent record is not mandatory.While creating a child record, a parent record is required.
Roll-up summary field not available.Roll-up summary field is available

Read more: Array methods in Salesforce Apex

Steps to create a Lookup relationship:

  1. From Setup, go to Object Manager.
  2. Select the object [which will be used to select the related parent record] .
  3. On the sidebar, click Fields & Relationships.
  4. Click New.
  5. Choose Lookup Relationship and click Next.
  6. For Related To, choose [Object with which it will be related – Parent Object].
  7. Click Next.
  8. For Field Name, enter [Name to depict the relation], then click Next.
  9. Click Next, Next, and Save.

Similarly, we can create other relationships as per our needs.

Setting Up Relationships in Salesforce

Define Your Data Model: Determine which objects you need to relate. Understand the data model and decide how objects like Accounts, Contacts, and custom objects should interact. Decide whether a lookup or master-detail relationship is appropriate based on your data needs. Use lookup for flexible connections and master-detail for tightly coupled relationships.

Create a Relationship Field: Go to the Salesforce setup menu and select Object Manager. Choose the object you want to create a relationship field for. Click on “Fields & Relationships” and then click “New”. Choose either Lookup Relationship or Master-Detail Relationship and click “Next”.

Configure Field Settings: Select the related object you want to link to. Provide a label and field name for the relationship field. Set additional options such as required status, reparenting (for master-detail), and lookup filters if necessary.

Set Field-Level Security: Specify the field-level security settings to control which profiles can see and edit the relationship field. Add the relationship field to the appropriate page layouts to ensure it is visible and usable for end-users.

Verify and Test: Save your new relationship field and verify that it appears correctly in the object. Create records and test the relationship to ensure it works as expected, linking the appropriate parent and child records.

Deploy Changes: If using a sandbox, thoroughly test the relationship setup before deploying to production. Use change sets or other deployment tools to move your configuration from sandbox to production, ensuring all relationships are correctly established.

Benefits of relationships

Enhanced Data Organization: Relationships structure data efficiently, linking relevant records.Improved Data Integrity: Enforces data consistency and accuracy by connecting related records.Simplified Data Management: Facilitates easier data handling through interconnected records.Comprehensive Reporting: Enables detailed and holistic reports by leveraging related data.Streamlined Processes: Automates workflows and processes by using relational data.Better User Experience: Provides users with intuitive navigation and access to related information.Scalability: Supports growing data needs with robust relational frameworks, maintaining performance and manageability.

Frequently Asked Questions (FAQs)

1. What are the three types of object relations in Salesforce?

In Salesforce, the three primary types of object relationships are:

  1. Master-Detail Relationship: This is a tightly coupled relationship where the detail (child) record cannot exist without the master (parent) record. The master object controls the sharing and security settings of the detail object, and when a master record is deleted, all related detail records are also deleted. Additionally, roll-up summary fields can be created on the master object to aggregate data from related detail records.
  2. Lookup Relationship: This is a loosely coupled relationship where one object can reference another. The child record can exist independently of the parent record, and deleting the parent does not delete the child records. Lookup relationships are used when the relationship between objects is not critical to their existence.
  3. Many-to-Many Relationship: This relationship allows records of one object to be linked to multiple records of another object and vice versa. It is implemented using a junction object, which has two Master-Detail relationships with the objects being linked. This setup is useful for associating multiple records from one object with multiple records from another object, such as students and courses.

Readmore: Permission Sets in Salesforce

2. How many lookup relationships are in Salesforce?

Salesforce allows up to 40 lookup relationships per object. This provides a great deal of flexibility in designing the schema and relationships between different objects in a Salesforce environment.

3. Can we convert master detail to lookup?

Yes, it is possible to convert a Master-Detail relationship to a Lookup relationship in Salesforce, but certain conditions must be met:

  1. No Roll-Up Summary Fields: The Master-Detail relationship cannot have any roll-up summary fields on the master object. These fields must be deleted before conversion.
  2. Mandatory Fields Check: Ensure that all detail records have values for the Master-Detail field before conversion.
  3. Setup Changes: Navigate to the object’s field settings, and you can change the relationship type from Master-Detail to Lookup.

After these steps, the Master-Detail relationship can be successfully converted to a Lookup relationship.

Read more: Loops in Salesforce Apex

4. What are the different types of lookup in Salesforce?

Salesforce provides several types of lookup relationships:

  1. Standard Lookup Relationship: This is the most common type where one object can reference another, and the child record can exist independently of the parent.
  2. External Lookup Relationship: This type of lookup is used to link a child object to a parent object that resides outside of Salesforce. It is particularly useful for integrating with external data sources.
  3. Indirect Lookup Relationship: This type of lookup is used to link an external object to a standard or custom object within Salesforce based on a custom field mapping. It allows relating data stored outside Salesforce with data inside Salesforce.
  4. Hierarchical Lookup Relationship: This is a special type of lookup relationship available only for the User object. It allows users to create a hierarchy within the User object, such as organizational hierarchies, for managerial structures and reporting relationships.

Readmore: Role in Salesforce

5. How do I create a relationship between two fields in Salesforce?

To create a relationship between two fields in Salesforce, you need to navigate to the Object Manager, select the appropriate object, and create a new field of type “Lookup Relationship” or “Master-Detail Relationship.” Follow the prompts to select the related object, configure field settings, set field-level security, and add the field to page layouts. This process establishes a link between the records of the two objects, enabling them to interact and share data.

6. How to use relationship in Salesforce?

Using relationships in Salesforce involves leveraging the connections between objects to access and manage related data efficiently. You can create reports and dashboards that include related records, automate workflows based on relational data, and use related lists to view associated records. For instance, a lookup relationship allows you to link a contact to multiple accounts, while a master-detail relationship ensures that child records inherit attributes from the parent record, maintaining data integrity and consistency.

Read more: SOQL Query in Salesforce

7. How do I add related objects in Salesforce?

To add related objects in Salesforce, you create relationship fields that link records from different objects. Navigate to Object Manager, select the object, and create a new relationship field (lookup or master-detail). Configure the relationship settings, including the related object, field label, and security settings. This setup allows you to associate records from the chosen objects, enabling seamless data interaction and management across the Salesforce platform.

8. How to create a lookup relationship in Salesforce?

To create a lookup relationship in Salesforce, go to Object Manager, select the object where you want to create the relationship, and click on “Fields & Relationships.” Choose “New,” then select “Lookup Relationship” as the field type. Follow the wizard to select the related object, configure the field label, set field-level security, and add the field to page layouts. This process establishes a flexible connection between the two objects, allowing you to link their records without enforcing strict dependencies.

9. How do I add a relationship to a contact in Salesforce?

To add a relationship to a contact in Salesforce, create a relationship field on the contact object. Navigate to Object Manager, select “Contact,” and create a new field of type “Lookup Relationship” or “Master-Detail Relationship.” Choose the related object, configure the field settings, and add it to the appropriate page layouts. This allows you to link the contact to other records, such as accounts or custom objects, enhancing data connectivity and management.

Read more: record types in Salesforce.

10. How do I link one field to another in Salesforce?

Linking one field to another in Salesforce typically involves creating a lookup or master-detail relationship. Navigate to Object Manager, select the relevant object, and create a new relationship field. Follow the steps to choose the related object and configure the relationship. This process connects the records of the two objects, enabling data sharing and interaction based on the defined relationship.

11. How do I create an account relationship in Salesforce?

To create an account relationship in Salesforce, navigate to Object Manager, select “Account,” and create a new relationship field. Choose “Lookup Relationship” or “Master-Detail Relationship” as the field type, then follow the prompts to select the related object and configure the field settings. Adding this field to the account page layout allows you to link accounts with related records, such as contacts, opportunities, or custom objects, facilitating comprehensive data management.

12. What are the six types of relationships in Salesforce?

The six types of relationships in Salesforce are Lookup Relationship, Master-Detail Relationship, Many-to-Many Relationship (using Junction Objects), Hierarchical Relationship (specific to the User object), Self Relationship (where an object relates to itself), and External Lookup Relationship (linking to external objects). Each type serves a specific purpose, from flexible links between records to tightly coupled parent-child relationships, supporting various data management needs within the Salesforce platform.

13. How do I create a relationship group in Salesforce?

Creating a relationship group in Salesforce involves defining how multiple objects interact and relate to each other within a common context. This typically requires setting up junction objects to manage many-to-many relationships. You start by identifying the objects you need to connect, then create a new junction object that holds the relationship details. Next, create master-detail relationships between the junction object and the original objects. This setup allows you to manage complex relationships and data interactions more effectively within Salesforce.

Read more: String methods in Salesforce apex

14. How to create a related list relationship in Salesforce?

To create a related list relationship in Salesforce, you need to establish a relationship field between the objects you want to link. Once the relationship field is created, you can add the related list to the page layout. Navigate to the Object Manager, select the parent object, and edit the page layout. From the page layout editor, drag the related list component to the desired section of the layout. This setup will display a list of related records within the parent record’s detail page, allowing for easy access and management.

15. How to find fields and relationships in Salesforce?

Finding fields and relationships in Salesforce involves using the Object Manager. Go to the Salesforce Setup menu, select Object Manager, and choose the object you are interested in. Under the “Fields & Relationships” section, you can view all existing fields and relationships for that object. This area provides detailed information on the field types, relationships, and their configurations, helping you understand how data is structured and connected within your Salesforce environment.

16. How do I add related cases in Salesforce?

To add related cases in Salesforce, you need to establish a lookup or master-detail relationship between the Case object and the related object. Navigate to Object Manager, select “Case,” and create a new relationship field. Configure the relationship settings and add the field to the Case page layout. Once set up, you can create and link related cases by using this relationship field, ensuring that cases are properly connected and easy to manage within the Salesforce platform.

17. How to create a relationship between objects in Salesforce?

Creating a relationship between objects in Salesforce involves defining a relationship field that links the objects. Navigate to Object Manager, select one of the objects you want to relate, and create a new field. Choose either “Lookup Relationship” or “Master-Detail Relationship” depending on your needs. Configure the field settings, select the related object, and set field-level security. Add the relationship field to the page layouts as needed. This process establishes a link between the objects, allowing their records to interact and share data efficiently.

Comments are closed.