Hello friends, today we are going to discuss about object relationship in Salesforce. Basically, it is about how objects are connected to each other. I have seen lots of students confused about this fundamental concept. So lets clear the concept right away.
This article will cover terms like:
- One-to-one relationship
- One-to-many relationship
- Many-to-one relationship
- Master-Detail relationship
- Lookup relationship
Before all the discussion, let’s take a look at basics. I know it will become a little bit boring but be patient, you will learn something exciting and also get more understanding of object relationship. So, If you are from computer science background, then you should definitely know about DBMS(Database Management System). For storing the object and object attributes, we use database. In the database, objects are stored in the form of row and column. The object’s name becomes a table name, attributes of the object become a column and object record are rows. For more understanding look the below figure.
In the database, tables are connected to other table using the foreign key. As you can see below image enrollment number work as a key for other tables.
But don’t worry Salesforce make it simple there is no concept of the foreign key. Salesforce provides a relation field.
Relation model:-
- One-to-one:- In one to one relationship, one object connected with only one object. It is a fundamental relationship between two objects.
- One-to-many:- In one to many relationship one object connect with many objects.
- Many-to-one:- Many to one relationship is the reverse of One-to-many relation. In this many objects are connected to one specific object.
You have read so far; I like your patience and your courage about learning. So, my friends, this was basic of relationship. Now we are going to discuss Salesforce relationship. Salesforce object relation is straightforward. So, don’t think much about it. Just keep in mind some point which I am going to discuss with you below. We use these relations as per our project requirement. Salesforce provides two standard relationships between objects. Lookup relationship and Master-Detail relationship.
Master-Detail relationship:- It is a parent-child relationship. Using the help of this relation, we create a dependency between objects. Child record is always dependent on parent object record, and master-detail field is compulsory when you are creating child object record. If we delete the parent record child records is auto-deleted. It is a one-to-many relationship. One parent can have multiple child objects, and all depended to one parent object.
Example:- You can take a simple example of class and student relationship. A class can contain multiple students, and every student belongs to a class. If we delete the class then there is no existence of their students. If any school there is no class of XII then there is no student of class XII in that school.
Some Limitation of Master-Detail Relationship:-
Salesforce allowed only two master-details on the object. Parent field is required on the child object. You can’t create a record when the field is blank. When the object is a child of another object, you can’t make that object as a parent of other objects. When you delete parent record, the child is deleted.
Lookup relationship:- It is an independent relationship between the object. We relate the object with other objects without dependency.
Example:- Lookup relationship is like your alternate number. You have seen many times when you fill any form, the alternate number is optional. So, you can fill it or not. It will not affect any other object. But it is related to you.
Some Limitation of Lookup Relationship:-
You can create only 25 lookup field on the object.
You can see all relation of the object on schema builder. Go to Quick Find >> Search “Schema Builder”>>Quick Find. Find the objects which relationship you want to see.
I hope you liked this article.For more of these kinds of articles please subscribe to the blog. Happy coding!
References: