In database we link one table with another table using join statements,Primary Key and Foreign Key so that we can access data of one table in another table but in Salesforce we don't use these statements,in order to establish a relation between fields(equivalent to column of table) of different object(table in database) we have 2 ways:
- Lookup Relationship Lookup create a relationship that links one object to another object.Lookup relationship can be used to create one-to-one(one record link with only one record of other object) and one-to-many relationship(one record link with many records of other objects) e.g. Suppose we want to create lookup relationship on a JOB Application and Position Records,many job application records can be related to a single position records.
- Master Detail Relationship This is a special type of relationship between two objects(child or detail) and another (parent or master).This type of Relationship closely links both the objects.e.g When we delete the master(parent) object automatically all child objects related to the parent gets deleted and the sharing rules of child is inherited from the parent object.These properties are not available in Lookup.
Steps to Create a Lookup Relationship
Go to setup and type objects on quick search box and click on object.
- Now you will see the list of objects select the object(child objects) and if you want to create new object you can create new object depends on you
- After selecting the object go to custom fields and relationship section and click "new" button to create new field
- Now you will see a list of type of field you want to create but now are interested only lookup and master detail that is highlighted in the photo and select any one and click "next" button,if you are creating this first time please read the description given on the right hand side.
- Now select the object from the list
- Now Enter the required details like field name,field label and also you can setup lookup filter if you want to restrict the records available in lookup.
- Now set the field level security for different Profile,The field will be hidden from all profiles if you do not add it to field-level security.
- Now add the created field to Page layout of the object,you will see the list of object's page layout on which you created the custom field and click "next" button
- In the Last Step Select the layout on which you want to add related list(the created field will be seen in the related list of other object tab),here you will see the parent object layout,if tab is not created first create create the tab in order to see the related list.
Junction Objects are used to create many to many relationship between any two objects. A many to many relationship allow each record of one object to link with each record of another object and vice versa.For example,you may have 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.
A Junction object is a custom object with 2 master detail relationships.
Creating Many to Many relationship consists of :
- Creating the Junction Object
- Creating the two master detail relationship
Creating the Junction Object
- Create a custom object(click setup first and then type "object" in quick search box and then select object )
- In the custom object wizard enter the required fields to create object,in the below picture 3 custom objects are created to create many to many relationship
Creating the two master detail relationship on Junction Object
- Create 2 master detail in Junction Object.
- Choose "Master Detail" in field Type and click "next".
- Choose the object in "Related To "Picklist in our case choose any one either "Bug" or "Case" object
- Now fill the Required details like field details,etc and further steps are similar as done above while creating the lookup,so I am skipping the further steps images(unnecessary)
- Similarly Create Master Detail of another object(in our case "Case" object) on Junction Object.
We have created the Many to Many Relationship using Junction Object but in order to check manually you need to create the tabs of both the custom objects "Bug" and "Case"(custom) object.To create a tab you need to search "tab" in quick search box in setup,click tabs then create new Tabs of both objects one by one.
Now After creating Tabs create 2-3 records on both tabs "Bug" and "Case" and open any one tab and select any record you will Junction Object in related list and click on "New Junction" and Create 2-3(many record) on Junction Object.
In this way we have created one to many relationship between bug(one) and Junction(many) object,Similarly vice-versa.
In this way we achieved the many to many Relationship between both "Case" and "Bug" object.