 Hello, friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to demonstrate you how we can create lookup and master detail relationships in Salesforce Lightning. You can search me on YouTube by my name. You can watch various Salesforce related videos on my channel there. So now we are logged in. So first let me explain you the lookup scenario that we are going to create. So basically lookup and master detail both are used to create one-to-many relationship. So we have two objects, student and book. So now I'm going to create lookup field on book object so that it can be linked to student object. So one student can have many books issued. So we are going to link book object with the student with the help of lookup relationship. So now I'm moving into org. So this is object manager. Let me open my university app also. So there you can see this is the student object tab. So we have details of students. Then we have books. So now I'm going to create lookup relationship field. So always remember that whenever we want to create lookup relationship. So we need to create lookup field on the second object which is on the many side. So I'm searching book object, clicking on new. So here you can see we have one data type that is lookup relationship. So first understand the details which are available here. So read it carefully. Lookup relationship creates a relationship that links this object to another object. So this object means book and to another object in our example that is student. So we are going to link book object with student object. Then further it says the relationship field allows users to click on a lookup icon to select a value from a pop-up list. So while creating book record, there will be a lookup icon. So whenever you click on that lookup icon, it will pop up a list of available student records. So you can select a particular student record so that that book record will be linked to a particular student record. Then further it says the other object is the source of the values in the list. So in our case other object is the student which is the source of values for book records. So suppose you are creating five book records. So you can select same student record for both for all five book records. So that's why it is known as one to many relationship. So there are options like you can select one student for five book records or you can do like five different students for five different books. So it depends on the requirement. So now we are going to create it. So I'm clicking on the next. Searching for student object. So this book object will be related to student. So label will be student. Now this is field level security. So I want to display this field on each profile and in editable mode. Then I want to place this field on the book layout. Now this is important. This is related to add custom related list. So whenever you create look of fields, so a related list will be available on the primary object. So in our case that is student. So we are creating look upon book. So book related list will be available on the student object. So you can also modify related related this label. So I'm putting it as books and clicking on save. So here you can see a look of field is created student. This is API name student underscore underscore C and it is showing it is look up to student. So now moving to applications. I'm doing a refresh. So now you will see on each book record there will be a look up. So first go to student. I'm opening a record. So this is the detail page of this record. I'm moving to related. So here you can see the related list. So right now this student record is not related with any book record. So now we are going to open book tab. So this time I'm opening existing record. So here you can see this look up is available. So if I click on edit. So now I will be able to select students from here. So I'm selecting because clicking on save. So this book is now related to because. So now if I open because record from student object and go to related list. So here you can see because is associated with this book. Right. Now let me create new book record. So I'm just putting book name and I'm selecting student and clicking on save. So while creating you can add student and while editing existing record you can also select students through the look up. So now if I open that because record and go to related list. So here you can see two books are associated. So this way I hope you understood how this one to many relation is working. So we created look up field on book object and every time whenever you are creating book record it should be associated with one of the students. So look up relationship is optional. If you don't fill it then it will not be linked with any other record. Like if we focus on this suppose I'm opening this record. So here you can see it is not associated with any student. So look up relationship is optional. If you don't fill it nothing will happen. And if you place a particular record here related to a particular object then it will reflect into the corresponding related list. So I hope you understood how we are implementing this look up relationship. Now moving to another relationship that is master detail relationship. So here again we need to implement one to many relationships. So on master side there is book and on detail side book history object will be. So this is master object. And this will be detail object. Right. So after explaining master detail relationship, I will explain you the difference between look up and master detail. So let me first create this master detail relationship field. Then we will compare both. So again if I open my application so here book is available and book histories are available. So both tabs related to both objects are available. Now, in this case, master detail field will be created on book history object. Right. So from this object manager, sorry, I am searching for book history, selecting field and relationship, clicking on new. So this time we need to select this master detail relationship. Let's understand the details which are available here. So it says creates a special type of parent child relationship between the object this between this object, the child or detail. So book history, this object on which we are creating master detail will be treated as child. Or you can say detail and another object will be treated as parent or the master. So in our case, other object is the book. So book will be the master object and book history will be the detail object. Then further it says the relationship field is required on all detail records. So this is mandatory field. Then the ownership and sharing of the detail record are determined by the master record. So whatever sharing settings are available for master record will be automatically available for the detail records. Then further it says where our user deletes the master record, all detail records are deleted. So if you delete book record, then the associated book history will be deleted automatically. So this is the major difference between lookup and master detail. In case of lookup, if you delete parent, then child will not be deleted. So right now I created one student and multiple books. So if I delete student, then book record will not be deleted. But in master detail case, if we delete book, then all the book history related records will be deleted. Then it says you can create roll-up summary fields on master record to summarize the detail record. So this is the additional feature of master detail relationship that is not available in the lookup. So in my next video you will see how we can create roll-up summary fields. So at last it says the relationship field allows user to click on a lookup icon to select a value from a pop-up list. The master object is the source of the values in the list. So this is similar to lookup. We will be having a lookup icon. If we click on that, source record list will be available and we can choose a particular record. So now I am going to create master detail relationship field. So parent will be book. So field label will be book. So here you can see the sharing settings. So it says select the minimum access level required on the master record to create, edit, or delete related detail records. So there are two options, read only and read write. So you can select any option that you want to. Then it says allow re-parenting. So this I am going to tell after demonstrating how master detail works. So I am leaving this right now. After completing this master detail field creation, again I will open this and explain this. And about this lookup filter you will get to know this in my another video that will be available in this playlist. So I am not covering lookup filter in this video. So here you can see field level security. So it should be visible because it is mandatory to fill master detail field. You cannot leave it blank. Then it should be available on the page layout. Then again, same as lookup relationship, the related list will be available on the book. So the related list label will be book histories. If you want to change it, you can. So now I am saving it. So it is showing an error. So it is a natural error. So let's leave it first then understand why it came. So it is saying you cannot create a master detail relationship on an existing custom object if records already exist. So now we need to check for book history. So here we have one record already created. So it is not allowing us to create master detail relationship. It says you must first create a lookup relationship, populate the lookup field with data in the record and then change the relationship type to master detail. Right. So this is the way if you have existing records, then you need to create lookup. Fill that lookup and then convert it into master detail relationship. Right. So in our case, we have only one record. So right now I'm deleting this. But in real time scenarios, if you have many records, then you cannot do this. You just need to follow these instructions. So how to convert lookup into master detail that also I'm going to explain in another video. So you can watch how we can convert lookup into master detail relationship. So now again, we need to follow the same steps. So I'm quickly completing these steps again. So this time you can see master detail relationship field is created and it is linked to book. It is created on book history. Now I'm moving to the app. So if I go to book, open any record, go to related list. You will see book histories related list is available here. If I create new book history, you will see a lookup is available here. So I am selecting issue date. I'm selecting return date. I'm selecting a book and clicking on save. So if I open this book record and go to related list. So here you will see that book history is available. Right now. If I create one more record of book history. So again, I have to open this and you will see in the related list two records are available. Right. So we have two records. Now if I open this, so you will see, I'm not able to edit this. Once I selected a particular book, I'm not able to edit this. So this is related to allow reparenting. So I'm opening book history object, opening this. And you will see the allow reparenting checkbox is unchecked here. So read this description. It says child records can be reparented to other parent records after they are created. So if I click on this checkbox and click on save. So now I will be able to edit that so that I can select different record for that master detail field. So here you can see this edit is available now if I click so I can remove and select another book. Right. So now if I go to book one, so in related list, only one book will be available. If I move to book number five, then in related one book is available. So now I think you understood about allow reparenting thing. Now let me clear one more difference between look up and master detail relationship. So if we delete parent record and look up, then associated records will not be deleted. So I'm opening student record. So now for the student because you can see two books record are associated book four and book five. So let's say if I delete this. So I deleted that record. Let's see what happens to book. So all the records are available here. Right. Now if I open book one book history three is associated with it. So now I am deleting book one. So this is master detail case. So if I delete parent recording case of master detail relationship now see what happens book history three record is deleted automatically. So friends, you need to remember this thing in case of look up relationship. If we delete the parent record associated child will not be deleted because this is loosely coupled relationship. But in case of master detail relationship, if you delete parent record, then all the child records will be deleted automatically because it is tightly coupled relationship. Then whenever you create master detail relationship field, always remember that either you don't have any record in the object. Otherwise, you need to create look up relationship first, then fill all the look up fields and then you need to convert that into master detail relationship. Right. So these things you need to remember while using master detail and look up relationship fields. And these are for one to many relationship. So I hope you understood whatever I demonstrated in this video. You can search me on YouTube by my name and you can follow more Salesforce related videos. Thank you for watching this video.