TYPES OF OBJECT RELATIONSHIPS
Object relationship in salesforce associate object with other object. Once the relationship is set between the objects, we can view the information from an object and related information on the same record. Object relationships in Salesforce consists of two basic types – master detail and look up relationships. The other types are based out of these types. They are many to many, indirect look up and external look up relationships. We will each of these object relationships with examples.
1. Master-Detail Relationship
Closely links objects together such that the master record controls certain behaviors of the detail and sub detail record.
If Master is deleted, Child records would be deleted automatically.
The child security access is derived from master
Rollup summary is possible
Example 1:
Suppose we are storing information of different schools in a custom object called “School” and the classes related to each School is stored in a custom object called “Class”. Then we may have records as below in the corresponding objects. These two objects are related to each other by a custom field called “School” in child object “Class”
Object: Student (API name – School__c)
ScohoolId SchoolName Address
S001 ABC School Chennai
S002 Veer School Hyderabad
S003 DAV School Hyderabad
S004 SIV School Bangalore
Object: Class
ClassId CName Seats School
C001 VI 30 S001
C002 VII 35 S001
C003 VIII 35 S001
C004 IX 30 S001
C005 VI 30 S002
C006 VII 35 S002
C007 VIII 35 S002
C008 IX 30 S002
C009 VI 30 S003
C010 VII 35 S003
C011 VIII 35 S004
C012 IX 30 S004
Hence the relationship field “School” is a foreign key which would have the values populated from primary key in parent object. (–> this is a custom relationship field created of type “Master-Child Relationship”).
This value in salesforce is termed as record id. Every record that created in salesforce will have record id generated automatically. Here, the SchoolId (from school object) may have the record id in the pattern a00i0000004JzIE (15 digit length) which is a primary key for Salesforce record that can be used to uniquely identify a record. Here the value S001 is given for an example but in real time this would be the record id.
So back to the mater child concept, now if we delete any school record, the class records will be get deleted automatically and we can also have the rollup summary field on parent record. Rollup summary field is to summarize/average/count the child information on a parent record like no of classes under particular school and total number of seats available in a school.
2. Lookup Relationship
Links two objects together. Lookup relationships are similar to masterdetail relationships, except they do not support sharing or rollup summary fields. With a lookup relationship, you can: Link two different objects.
Example 1:
Campaign (Child)
– website
– webinar
Venue (Parent)
– address
This case, the Campaign can exist without parent also (means without venue).
3. Many-to-Many Relationship
We can use master detail relationships to create many to many relationships between any two objects. A many-tomany relationship allows each record of one object to be linked to multiple records from another object and vice versa.
Example:
A Custom object called “Bug” that relates to the standard case object such that a bug could be related to multiple cases and a case could also be related to multiple bugs.
Instead of creating a relationship field on the Bug object that directly links to the Case Object, we can link them using a junction object. A junction object in salesforce is a custom object with two master-detail relationships, and is the key to making a many-to-many relationship.
For example, if you wanted to use a junction object to create a many-to-many relationship between bugs and cases, you could name the junction object BugCaseAssociation
4. Hierarchical Relationship
A special lookup relationship available for only the user object.
It lets users use a lookup field to associate one user with another that does not directly or indirectly refer to itself. For example, you can create a custom hierarchical relationship field to store each user’s direct manager
5. External Look up
An external lookup relationship links a child standard, custom, or external object to a parent external object. When you create an external lookup relationship field, the standard External ID field on the parent external object is matched against the values of the child’s external lookup relationship field. External object field values come from an external data source.
External ID – A field can be marked as external id field which means it can have values to denote the external system.
Example:
It can store oracle id to denote the records populated from external oracle system. So you can refer data in oracle system using this external id
6. Indirect lookup
An indirect lookup relationship links a child external object to a parent standard or custom object. When you create an indirect lookup relationship field on an external object, you specify the parent object field and the child object field to match and associate records in the relationship. Specifically, you select a custom unique, external ID field on the parent object to match against the child’s indirect lookup relationship field, whose values come from an external data source.
Hence An indirect lookup relationship is a new field type introduced with Salesforce Connect. It links an external object to a standard or custom object in the same way that a regular lookup relationship links standard or custom objects.
Conclusion
This concludes creating a Salesforce Object relationships is nothing but creating a custom field of a type master-detail or look-up in an object (consider as a child object) and relate the same object to a parent object. In general, the above custom relationship field is called a foreign key. Hence this field will hold primary key of a parent record for child record we relate. The primary key is always going to be ID of a parent record. The ID is a combination of 18 character length of alphanumeric characters.