Thursday, 28 November 2013

Object Relationships in Salesforce

A relationship is a bi-directional association between two objects. Relationships allows us to create links between one object and another. The platform supports following relationship types
  • Master-Detail
  • Lookup
  • Many-to-Many
Master-Detail (1:n)
A parent-child relationship in which the master object controls certain behaviors of the detail object.
  • When a record of the master object is deleted, its related detail records are also deleted.
  • The Owner field on the detail object is not available and is automatically set to the owner of its associated master record. Custom objects on the detail side of a master-detail relationship cannot have sharing rules, manual sharing, or queues, as these require the Owner field.
  • The detail record inherits the sharing and security settings of its master record.
  • The master-detail relationship field is required on the page layout of the detail record.
  • 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.
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 Leadobjects are the master.
Lookup Relationship (1:n)
This type of relationship links two objects together, but has no effect on deletion or security. Unlike master-detail fields, lookup fields are not automatically required. When you define a lookup relationship, data from one object can appear as a custom related list on page layouts for the other object. See the Salesforce online help for details.
  1. Child row not automatically deleted when a parent row is deleted
  2. No inherited sharing.
  3. 25 lookup relation relationships allowed per object.
  4. Lookup field on child not necessarily required. 
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.

Interview Questions about object relationships in Salesforce:
 How many relationships included in SFDC & What are they? 
We are having three types of relationships, they are
Lookup Relationship
Master-Detail Relationship
Many-to-Many relationship 
 What is a “Lookup Relationship”?
Up to 25 allowed for object
Parent is not a required field.
No impact on a security and access.
No impact on deletion.
Can be multiple layers deep.
Lookup field is not required.
 What is “Master-Detail 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. Rollup summary fields can only be created on Master records which will calculate the SUM, AVG, MIN of the Child records.
Up to 2 allowed to object.
Parent field on child is required.
Access to parent determines access to children.
Deleting parent automatically deletes child.
A child of one master detail relationship cannot be the parent of another.
Lookup field on page layout is required.
Does an object can have both relationships (Lookup Relationship & Master Detail Relationship) at a time?
Yes, single object can have both relationships at a time.
What is a “Self Relationship”?
A self Relationship is a lookup relationship to the same object.
Suppose let’s take an object “Merchandise”. Here we can create relationship in between the Merchandise to Merchandise (same object) object. That is called “Self Relationship”.
What is “Many to Many Relationships”?
Allow for the relationship of two objects in a many-to-many fashion.
How we achieve the “Many-to-Many Relationship”?
By using Junction Object we can achieve this relationship, here junction object is having Master- Detail Relationship with different objects (Ex.Students & Courses). Using this Master to Detail Relationship, we can create the Many-to-Many Relationship in between the objects.
 What are the main things need to consider in the “Master-Detail Relationship”?
Record level access is determined by the parent,
Mandatory on child for reference of parent, cascade delete (if you delete the parent, it can cascade delete the child).
What are the main things need to consider in “Lookup Relationship”?
Loosely coupled, not going to have all the above rules which are existed in master detail relationship.
Self relationship is always lookup to self relation object type. In many to many we actually create a junction object in the middle, it does lookup for the bound objects that are ultimately being bounded in a many to many way.
Can we convert the lookup relationship to Master Detail relationship?
Yes, We can convert the lookup relationship to master detail relationship only if all the existing record has valid lookup field values.
Can we create Master Detail relationship on existing records?
No. first we have to create the lookup relationship then populate the value on all existing record and then convert it.

No comments:

Post a Comment