Wednesday, November 7, 2018

Lookup, Master-Details and Many to Many Relationships in Salesforce.com

Lookup relationship:
1) Lookup relationship provides One to Many relationship between the objects by default. (i.e. A parent record can have Many associated child records.)

2) Upon creating the child record, lookup field is optional by default. (i.e. Lookup field is not required upon creating the child records)

3) Lookup relationship allow re-parenting of the associated child records by default. (i.e. we can change the parent of the child records)

4) Upon deleting the lookup parent record, associated child records will not be deleted.

5) We can make the lookup field mandatory by selecting "Required" checkbox, when creating the relationship field on child object.

6) To delete parent object of lookup relationship having associated child record is not allowed. (To delete it, first we need to re-parent all associated child records with other parent record)

7) We can create the lookup relationship on an object which already contains records.

8) We can have max. of 40 lookup relationships per an object.

9) In lookup relationship, parent and child records will have the owner fields (both records having owner). 

10) Sharing setting and security settings of parent will not enforce child and vice-versa.

11) Standard object can only be a child of custom object in lookup relationship only.


Master-Detail relationship:
1) Master-Detail relationship will provides one-to-many relationship between the objects by default. (i.e. A parent record can have zero or more child records)

2) We can't create the Master-Detail relationship on a child objects already having records.

3) Upon creating the child record, selecting the parent record is mandatory. (i.e. Lookup field is a required field)

4) Re-parenting option is not allowed in Master-Detail association by default(We need to enable it manually, by selecting the "Allow Re-Parenting" checkbox upon creating the field.)

5) An object can have max. of 2 Master-Detail relationship fields.

6) Only parent record will have the "Owner" field but child records will not have the "Owner" field. (i.e Parent record and child record will have same owner)

7) Sharing settings and Security settings of the child record will be depends upon the parent record.

8) Upon deleting the parent record, all the associated child records will deleted automatically.

9) Standard object can not be child of Custom object in Master-Detail relationship.


Predefined Lookup and Master-Details relationship between standard objects:

The relationship between standard objects are either Lookup or Master-Detail.

1) Account and Contact:
Between Account and Contact, Salesforce provides the Lookup Relationship.

Account --> Parent
Contact --> Child

Note1: We have lookup field "AccountId" on child object (contact), which is Id of parent object (Account).

Note2: Upon deleting account record, all the associated contact records will get deleted automatically.

Note3: We can prevent the deletion of contact records by using "Triggers". (i.e. Before deleting the Account, We need to get all the associated Contacts and make the "AccountID" field value as blank.) 

2) Account and Opportunity:
Between Account and Opportunity, Salesforce provides "Master-Detail" association.

Account --> Parent
Opportunity --> Child

Note1: We have lookup field "AccountId" on child object (Opportunity), which is Id of parent object (Account).

3) Account and Case:
Between Account and Case object, we have Lookup Relationship.

Account --> Parent
Case --> Child.

Note1: We have lookup field "AccountId" on child object (Case), which is Id of parent object (Account).
Once We map the association between the objects by using either Lookup or Master-Detail, in future we can change the relationship type from Lookup to Master-Detail and vice-versa.

Many to Many relationship:

In few cases, we have to map Many to Many relationship between two objects, which provides One to Many association from both the sides.

Ex: Let's consider two objects Mobile and EarPhone, where a Mobile supports multiple EarPhone and vice-versa.

In Salesforce, we can't map Many-to-Many relationship between two objects directly. Therefore, to achieve this property, we need to create a 'Junction object', which will have Master-Details relationship with both objects.

Predefined Many to Many relationship between standard objects:

1) CampaignMember: Between Campaign and Contact object. 

2) OpportunityContactRoles: Between Contact and Opportunity object.

3) OpportunityLineItem: Between Opportunity and Product object.

4) PricebookEntry: Between Product and Pricebook object.










No comments:

Post a Comment

Validation in flow input text field in sfdc: Maximum 255 characters limit

The input text field validate in sfdc flow behaves opposite of the generic sfdc validation rule.  Here the validation formula is evaluating ...