Friday, 27 July 2018

Relationships available in Salesforce

A relationship is an association among the objects. We can categorize the relationships into three kinds
1. One-to-One, 2. One-to-Many, and 3. Many-to-Many. But Salesforce supports only One-to-Many relationship in 2 flavors among the sObjects.
Salesforce offers One-to-Many relationship as following types,
1. Master-Detail relationship
2. Lookup relationship

In addition to those, Salesforce offers two more Special relationships called Self relationship, and Hierarchical relationships.

Though, Salesforce doesn't offer Many-to-Many relationship, we can achieve this using two One-to-Many relationships(We discuss it later).

1. Master-Detail relationship
            An association which tightly couples the Parent(Master) object and Child(Detail)  object [and Sub Detail object] is called Master-Detail relationship. What does Tight coupling means is,
     i. When a record in Parent is deleted, the corresponding related record in the Child and Sub Detail 
        are also deleted (Cascading delete).
    ii. Security settings for the Child record are inherited from Parent i.e., the same security settings 
        specified for Parent are applied to Child records.
   iii. Records in Child objects don't have Owner field, as these records are tightly coupled with Parent 
        record. So the Custom objects on Detail and SubDetail side cannot have Sharing Rules, Manual 
       Sharing, and Queues which require Owner field.

By default we cannot reparent the records in Detail and SubDetail objects. If we want to allow Reparenting the Detail and SubDetail records, we should specify it when we defining the Master-Detail relationship by checking the "Allow Reparenting" checkbox.

Reparenting: Changing the Parent of a Child record is called Reparenting.
Ex: Suppose Parent object have ABC, XYZ records, and we created a child record for ABC record, changing the parent from ABC to XYZ in child object is called Reparenting. So the child record now associated with XYZ not ABC.

Roll-Up Summary fields:
      Fields which rolls up the data in child records are called Roll-Up summary fields. These are created on Parent object. We can roll up child's data in four ways, and we can apply roll up on all records or only a set of records that match the criteria you define.
          i. COUNT - counts the number of records that match a specific criteria or all records
         ii. SUM - Sum's up the data in child records which meet the specified condition or all records
        iii. MIN - returns the Minimum value among all records or specified records
        iv. MAX - return the Maximum value among all records or specified records

Other features:
   i. It is defined on Detail or SubDetail object
  ii. Master-Detail relationship is a required field, hence always included in Edit page of detail object.
 iii. Cannot be removed from page layout
 iv. At most two Master-Detail relationships are allowed per object
  v. Standard object cannot be at Detail side when Custom object is on Master side.

2. LookUp Relationship
        An association which loosely couples two objects (Parent and Child) or an Object to itself is called a LookUp relationship.
Loose Coupling: 
    i. No Cascading delete,
   ii. Parent and Child can have different security settings,
  iii. Child objects can have Sharing Rules, Manual Sharing, and Queues.

Differences between LookUp and Master-Detail relationship:
   i. LookUp relationship do not support Roll-Up summary fields
  ii. It is optional

Other features: 
  i. We can make a lookup field required on pagelayout,
 ii. We can make lookup field required when defining it,
iii. If the field is optional, you can specify one of the following 3 actions to occur when parent record
    is deleted.
     i). Clear the value in lookup field (default)
    ii). Don't allow deletion of lookup record that's part of lookup relationship,
   iii). Cascade delete (this is available only by contacting Salesforce, and also only if the custom
         object contains the lookup relationship, however the lookup object can be either Standard or 
 iv. We can create a maximum of 25 lookup relationship fields per object.

2.1 Self Relationship
        An association which binds the object to itself is called Self Relationship. A record in an object is linked  to another record in the same object.

2.2 Hierarchical Relationship
        An association which binds the object to itself is called Hierarchical Relationship.

Differences between Self and Hierarchical relationships are:
1. Self relationship can be used on any object,
2. Hierarchical relationship is only available for User object, to represent Employee-Manager
3. In Self relationship, a record may reference itself indirectly, but in hierarchical a record should not
    reference itself directly or indirectly.

3. Many-to-Many relationship
        An association which binds two objects such a way that each record in two objects may have relationship with one or more records in other object.

      Salesforce supports this relationship through a Junction Object, which is defined as "a custom object with two master detail relationships to link two objects(custom or standard)".
Ex: Consider a situation where a Professor can teach many subjects and a Subject can be taught by many Professors.  So there exists a many-to-many relationship between Professor and Subject objects. To setup this association in Salesforce, we need to create an object called Teaches object. So the association looks like Professor-Teaches-Subject.

We know, the sharing and security settings are inherited from the parent to child in Master-Detail relationship, as we have two master detail relationships on Junction object, which master object settings are applied to junction object???

Primary Relationship: A master-detail relationship which is created first on the junction object.
Secondary Relationship: A master-detail relationship which is created second on the junction object.

The primary relationship determines the look and feel, Security and Sharing settings for the junction object. A master object to which the first relationship is created, junction object inherits that object settings. What happens if a Primary relationship is converted to a lookup or deleted??? automatically the secondary relationship becomes Primary Relationship, no manual effort required to make it Primary.

No comments:

Post a Comment