Salesforce Object Relationship

Relationship in salesforce

You can establish relationships between objects in Salesforce. You can associate one object with another. A relationship is very important to connect objects.

Different types of relationships between objects in Salesforce determine how they handle data deletion, sharing, and required fields in page layouts.

  • Master-detail relationship
  • Lookup relationship
  • Self-relationship
  • External lookup relationship
  • Indirect lookup relationship
  • Many-to-many relationship (junction object)
  • Hierarchical relationship

Master-Detail Relationship

It is a strongly coupled relationship between two objects. In this one object will be on the master side and another one is on the detail (many ) sides.

An important consideration for Master-detail relationship

  1. Deleting a detail record moves it to the Recycle Bin and leaves the master record intact; deleting a master record also deletes related detail and subdetail records. Undeleting a detail record restores it, and undeleting a master record also undeletes related detail and subdetail records. However, if you delete a detail record and later, separately, delete its master record, you cannot undelete the detail record, as it no longer has a master record to relate to.
  2. By default, records can’t be reparented in master-detail relationships. Administrators can, however, allow child records in master-detail relationships on custom objects to be reparented to different parent records by selecting the Allow reparenting option in the master-detail relationship definition.
  3. The Owner field on the detail and subdetail records is not available and is automatically set to the owner of the master record. Custom objects on the “detail” side of a master-detail relationship can’t have sharing rules, manual sharing, or queues, as these require the Owner field.
  4. Detail and subdetail records inherit security settings and permissions from the master record. You can’t set permissions on the detail record independently.
  5. The master-detail relationship field (which is the field linking the objects) is required on the page layout of the detail and subdetail records.

Lookup relationship

It is a loosely coupled relationship among Salesforce objects, which means even if a parent record gets deleted, the child records remain in the system

An important consideration for Lookup relationship

  1. When you create a lookup relationship, you can set these options:
  • Make the lookup field required for saving a record, requiring it on the corresponding page layout as well.
  • If the lookup field is optional, you can specify one of three behaviors to occur if the lookup record is deleted:
    1. Clear the value of this field This is the default. Clearing the field is a good choice when the field does not have to contain a value from the associated lookup record.
    2. Don’t allow deletion of the lookup record that’s part of a lookup relationship If you have dependencies built on the lookup relationship, such as a workflow rule, this option doesn’t allow the lookup record to be deleted.
    3. Delete this record also Available only if a custom object contains the lookup relationship, not if it’s contained by a standard object. However, the lookup object can be either standard or custom. Choose when the lookup field and its associated record are tightly coupled and you want to completely delete related data. For example, say that you have an expense report record with a lookup relationship to individual expense records. When you delete the report, you probably want to delete all the expense records, too.
    Choosing to Delete this record also can result in a cascade-delete. A cascade-delete bypasses security and sharing settings.

Self Relationship

Self-relationship is another example of a lookup relationship. In Salesforce, we can use lookup relationships to create self-relationship among objects; we can have a maximum of 40 self-lookups. For example, a campaign record can have a Parent Campaign record.

External lookup

An external lookup relationship links a child standard, custom, or external object to a parent external object.

Indirect lookup

An indirect lookup relationship links a child external object to a parent standard or custom object.

Many to Many Relationship

You can use master-detail relationships to model many-to-many relationships between any two objects. A many-to-many relationship allows each record of one object to be linked to multiple records from another object and vice versa. For example:Gym can have multiple member and a member can have multiple Gym.

One object can have only two master-detail relationships.

When modeling a many-to-many relationship, you use a junction object to connect the two objects you want to relate to each other.

consideration on Many to Many relationships

  1. Primary Relationship: whichever fields you create first in the junction object will become the primary field relationship and the object which is associated with it will be the primary master object.
  2. Secondary Relationship: whichever fields you create second in the junction object will become the secondary field relationship and the object which is associated with it will be the secondary master object.
  3. The owner filed in the junction object record will be derived from the primary master object.
  4. look and feel will also be derived from the primary master object.
  5. Security and sharing will be derived from the primary master object as well as the secondary master object.

Hierarchical relationship

In Salesforce, only a user object has this type of relationship where we can create a hierarchy of users in the organization. For example, a user can have his manager, and his manager may have senior manager, and so on till the CEO or CIO level.

Please visit my GitHub account for LWC core concepts.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ranbir Kumar Das

Ranbir Kumar Das

I M Believer, Helper, Chaser, Thinker, Rich, Explorer, Prayer, Boss, Freedom, Fearless, Investor, Faith, Creator, trillionaire, CSM, Salesforce certified