Tuesday, December 20, 2022

Relationships In Salesforce

* To avoid redundancy and inconsistency we have to use relationships in sfdc. 
* Redundancy: Repeating records/data in multiple locations is called redundancy.
* Inconsistency: Due to the repetition of the data, if we delete data at one location in other  location the data exists due to that inconsistency will arise.
 In SFDC, we have 5 types of relationships:
1. Self Relationship
2. Look-up Relationship
3. Master Detail Relationship
4. Many to Many Relationship
5. Hierarchical Relationship
 ********
1. Self Relationship:
*It is the relationship between two fields of the same sObject.
* From one field value of the sObject we will derive another field value of the sObject.
* Using Formula data-type we can achieve self relationship. 
2. Look-up Relationship:
* It is the relationship between two sObjects provided one is child which contains link to the parent and another one is parent which contains physical record.
 *If we delete physical record in parent object all the referencing child fields(not entire record) get deleted.
*If the relationship is not mandatory in that case we have to use look-up relationship.
* Up to 25 look-up relationships we can create for single sObject. 
3. Master Detail Relationship:
* It is the relationship between two sObjects provided one is child which contains link to the parent and another one is parent which contains physical record.
*If we delete physical record in parent object all the referencing child records(entire records) also get deleted.
*If the relationship is mandatory in that case we have to use master-detail relationship.
* In master-detail parent object we can create roll-up summary field.
*It is not possible to create master detail relationship if the child object has records in it, if it is empty then only we can create master-detail relationship.
* We can convert master-detail to look-up easily without any restrictions.
* up to 2 master-detail relationships we can create for single sObject.
* a detail record automatically inherits the sharing setting of its parent.
4. Many to Many Relationship:
*If one sObject having two master-detail relationships is called Junction Object.
*For a junction object no need to create tab.
* Junction object can't be on the master side of another master-detail relationship.
*The relationship between two master objects having a common child object i.e. junction object is called many to many relationship.
*many to many relationship form indirectly. 
*We can't create a many to many relationship, If two master-detail relationships on the junction object can't have the same master object.
*To exchange data between two master objects using many to many relationship we have to use related list.
5. Hierarchical Relationship:
* It is the relationship between the users of the organization.
****************
Important points to consider:
* We can convert look-up to master-detail only when all the fields in the look-up relationship populate (fill) with the values.
*If we delete a child sObject and undelete it again then all the master-detail relationships will be converted to look-up relationships.
*It is not possible to delete child sObject if the parent has Roll-up summary field, because parent is using child object information.
*It is not possible to delete parent objects because child sObject using parent sObject information (master-detail parent object).
* If the custom object is on the detail(child) side of the master-detail relationship, it can't be the master side of a different master-detail relationship.
* Standard objects can't be on the detail side of a custom mater-detail relationship.

***************************
Interview Questions:
Q. When to use Look-up and Master-detail relationships?
A.  * If we need to create roll-up summary field and if we need many to many relationship in that case we have to use master-detail relationship.
*For master-detail, relationship is mandatory.
*For look-up, relationship is not mandatory.
*First we have to create look-up then if the master-detail relationship is necessary for that sObject then we have to convert it to master-detail relationship.


No comments:

Post a Comment