Relationships in salesforce

You can establish relationships between objects in Salesforce. You can associate one object with another.

Salesforce provides the following types of relationships that can be established among objects:

  1. one to one relationship  :
  2. lookup relationship :
  3. master detail relationship :
  4. many to many  relationship :

What is “Master-Detail Relationship”?

Master-Detail relationship  is (1:n) one to many relationship.

Master-Detail relationship is the Parent-child relationship. In which Master represents Parent and detail represents Child. If Parent is deleted then Child also gets deleted. Roll-up summary fields can only be created on Master records which will calculate the SUM, AVG, MIN of the Child records.

In this type of relationship, the parent record controls the behavior of the child record regarding visibility and sharing. It means the security setting of a parent object applies to the child object.

You can define master-detail relationships between custom objects or between a custom object and a standard object. However, the standard object cannot be on the detail side of a relationship with a custom object. In addition, you cannot create a master-detail relationship in which the User or Lead objects are the master.

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. Here, both the parent and child have their own sharing settings and security controls. 

Difference between master-detail and lookup relationships

Lookup relationship

Master-detail relationship

Loosely coupled.

Strongly coupled.

Roll-up summary field cannot be created.

Roll-up summary field can be created.

Parent record is not required while creating a child record.

Parent record is always required in order to create a child record.

Lookup fields are not required on the page layout of the detail record.

Master-detail field is always required on the page layout of the detail record.

Standard object record can be on the detail side of a custom object in a lookup relationship.

Standard object record cannot be a child.

By default record ownership of child records is not controlled by the parent. 

Parent controls the record ownership of child records. The owner field is not available on the detail record in master-detail relationship queues, sharing rules and manual sharing is not possible for detail records as it requires the owner field.

You can have a child record without a parent.

You cannot have a child record without a parent.

You can have a maximum of 40 lookups on an object.

You can have a maximum of two master details on an object.

No cascade delete.

Cascade delete.

Many-to-Many

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.

To create a many-to-many relationship, simply create a custom junction object with two master-detail relationship fields, each linking to the objects you want to relate.

Record ownership: The junction object records inherit the value of the Owner field from their associated primary master record. Because objects on the detail side of a relationship do not have a visible Owner field, this is only relevant if you later delete both master-detail relationships on your junction object.

Junction objects are used to create many to many relationships between objects. If you take the Recruiting application example, you can see that a Position can be linked to many Candidates, and a Candidate can apply for different Positions. To create this data model you need a third object "Job Application" that links the 2.




One to One Relationship :


  • One-to-one:- In one to one relationship, one object connected with only one object. It is a fundamental relationship between two objects.

But, is there a One to One relationship in Salesforce. Well, it is not available out of the Box.
But is definitely achievable using simple workflow rules...

Scenario:

Let's consider the scenario that we would like to establish a One to One relationship between
Accounts and Contacts..

Step 1:

Create a custom field on the Contact object. Make the field unique.
This field would be used to hold the ID of the associated Account.

Step 2:

Create a Workflow rule on Contact. Update the custom field with the value of the associated Account ID.

Done!! you have established a one to one relationship between Account and Contacts...

When you try to add a second contact to the Account, the "unique" constraint would be 
violated and an error would be thrown.....

  • Create a master detail relationship on PAN_Card__c to Employee__c object.
  • Create a roll up summary field on Employee__c object of PAN_Card__c with count type.
  • Create a validation rule on Employee__c object rollup summary field to check if count > 1.

In this way also, We have established a one to one relationship between PAN_Card__c and Employee__c. So it will throw an error if Employee__c has more than one PAN Card.

Relationship between Account and contact


 

  


account and contact behaves as Master-Detail in business logic but on UI it is a 

lookup relationship 




You can create a contact without filling account i.e it shows that there is a lookup relationship between account

 and contact.



If you have created a contact with account and you delete that account then contact will be deleted, this shows

 that it is in Master-Detail relationship. This is a standard behavior

So we can say it in both ways, but it documentation it is a lookup relationship.












    Comments

    Popular posts from this blog

    Security model

    EVENTS IN Lightning Web Components

    Lightning web Components.