 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. Here you can see my website studiesalesforce.com so where I placed the videos which I created related to Salesforce so you can go and watch. So in this video we are going to discuss about related records in Apex. So what we will be discussing I will tell you how we can insert related records through Apex. How we can update the related records and how we can query related records like from parent object to child and from child to parent. So first I'm going to explain how to insert related records. So I have already opened this anonymous window. So I'm going to execute so you can click on debug and open execute anonymous window. So here you can write Apex instructions directly and you can execute them. Later on you can implement classes as well and through those you can call the methods. So those classes generally we implement when we call the Apex classes through triggers. So in this video we are just understanding the Apex code so we are not implementing any trigger so those code will be directly executed this anonymous window. So what we are going to do first we are going to create one account and then with related to this account we are going to create a contact. So account ACC equals to new account and inside this parenthesis I'm putting name. So name I'm putting as Sanjay Gupta Tech school. Then second field is phone. So in phone I'm putting 123456 and then I'm writing insert ACC. So when the statement will be executed. So this account record will be inserted and the ID will be available into this ACC. So further I can use that ID to create contact. So. So here I need to put account ID equals to ACC.ID then first name equals to Sanjay. Then last name equals to Gupta and phone equals to 123456. Right. And now I can write insert. So first account will be inserted. So that ID will be available into ACC. Then we are fetching that ACC.ID and we are relating this content newly created contact with account. So now I'm going to execute this. So I need to click on this execute button. Okay, so an error is there. Okay, I forgot to put comma here. So that's why that error occur. Okay, so here you can see this is showing exception field custom validation exception exception please populate it populate value of active underscore underscore C. So this is a validation rule. So I need to put active underscore underscore C equals to yes. So these are the values for account. So again I'm going to click on execute. Yeah, this time executed successfully. So I'm moving to account. So whatever you perform through anonymous window that also reflect in the org. So here you can see this account, Sanjay Gupta underscore Sanjay Gupta tech school is created, and it is having related contact as well. So phone is available if we see the details of account so you can see here also phone number is available. So I hope you understood how we can create related records to apex, then I'm going to tell you how you can update the related records. So, again, I'm opening an anonymous window so this time I'm first going to query the records and then I will be updating so we're moving all these so I'm going to click on equals to when I'm applying select ID name and phone these and fetching for contact and for account. I'm just fetching phone. I'm sorry, contact where name equals to Sanjay Gupta ID equals to null. Right, so those details will be varied. And here, let me put one more condition and com.name equals to Sanjay Gupta tech school. Right, so all the details will be available in the pond. So now I can write corn dot phone equals to so you saw right now we created both a count and context and we provided phone number is 123456. This time I'm putting a triple one for corn dot phone and corn dot account dot phone. I'm putting 222 six times. Then I'm writing update corn and then I'm writing update corn dot right and then I'm executing this. So moving here again I'm refreshing. So here you can see for account details, phone number is triple to sorry, six times two and for contact you can see six times one are available. So this way, you can update related records. So we updated contact phone as well as we updated related account phone with this. So I hope this way you understood how we can insert related records how we can update related records. Now, next is query related records. So first I'm going to tell you how we can query parent to child. So this we will be applying on standard as well as custom objects. So I'm removing this. So what I'm going to do, I'm going to implement a for loop. So we are applying. So, okay, let me first create a list of account. ACC list equals to list of account. And in this ACC list, I'm going to query. So select ID name. Now, this is we are applying query on parent because account is on parent side and contact will be on child side. So I will be carrying contact now. So select ID name from context now from where this context came. So what you need to do you need to go to child object, then in field and relationship search for the look up to parent. So here you can see look up to account is available, open this. And here you need to search for the child relationship name. So that name you need to use while querying the child reports. Now from account. So this way we can apply the query. Then where name equals to. So, so this is the record we right now created so that will be carried along with the contact that is available with that account. So now we can write system dot debug. And here I can write ACC list and ACC list. Right. So now I'm going to execute this. So the result will be displayed in the debug log. So here you can see this is account ID and name are available. Now, if we want to display contact details, so we need one more system dot debug. And here we can write contacts and you can write ACC list. Okay, ACC list is maybe containing more than one account so we can iterate a loop then. So you need to apply if condition then not ACC list dot is empty. So you to check if list is not empty then we can apply a follow. So account ACC then ACC list. So one by one account records will be available into ACC. And now you can apply a system dot debug contacts plus then ACC dot contacts. So let's say you query two accounts and each account is having two related contacts. So one by one account details will be available into this ACC and the particular account related contact will be displayed with this. Right. And later on, maybe if you want to apply some operation on the related contact context as well. So you can apply one nested loop as well. Like this ACC dot contacts. Right. So you can write the instructions whatever you want to perform here. So this way you can create a relationship between parent to child. So we queried child records through parent. So now if I execute this so you will see contact details as well. So it is going to open debug logs. So here you can see in first line account details are available and the related contact is available here. And if you match these IDs, so account ID and here in contact account ID, both are same. Right. So this way you can query the data. Right. And later on what you can do instead of creating this list. Let's say I'm commenting this, this as well and this as well. And from here I'm copying this and I'm putting it here. So what I did. I'm just applying the query inside the loop. So if we do so, so what we can remove, we can remove this. We can remove this. And this. So no, if condition is required because we are not having any account list this time. So whatever records we are querying, they are directly available in the loop and then we are doing the operations. Right. So this is on standard object. Now, let me show you how we can apply this on custom objects. So I'm removing all these things. Yeah, uncommented. Now, in my org, I have two custom objects student and book. So student is on parent side and book is on child side. So here you can see if I go to fields and relationship or book object. So you will see one. Lookup relationship to student. So here, if I open this so we can identify the child relationship name. So that is books. So now how we can use this. So student underscore underscore C. So it is the parent one. So first time putting it, then this will be student list. So I'm putting it everywhere. Now, from student I'm fetching ID name, then select ID name of books. So child relationship name, and also remember we need to put underscore underscore are as well if you are fetching custom object as a child. So the student where name equals to so student name I'm putting Sanjay. Right. So I have one student here whose name is Sanjay. And the student is having some related books. So here you can see two books are related. So those related books will be fast. And here we have a checking student list is not empty. Here also student underscore underscore C STD. So here student list will be displayed now one by one student record will be available into STD. And here I can put books. And later on if I want to iterate it. So here I can write book underscore underscore C B then STD dot books underscore underscore R. So this is through child relationship name and here we are creating one variable of this book object. So that's why book underscore underscore C. So this way you can apply this parent to child relationship on custom objects. So if I execute this. Okay, spelling mistakes, so we are student. So this is executing and the results will be available in the debug logs. So here you can see first student details are available in second line you can see book details. So total two books are related with that student. So first book name is 0005 second book name is 006. And here you can see that. Right. So this way you can perform operations on related reports by calling them. So this was parent to child. Now, the time is for last one that is child to parent. Right. So let me show you how we can do that. So first I'm going to show you standard one. So on child side we have contact and on parent side we have accounts so I'm writing contact. Con equals to select ID name. Then you need to write parent side object dot the field. So whatever parent is related whose name we want to fetch from contact. We have name equals to Sanjay Gupta. Right. So details will be available in the contact now. So let me make this list of contact because they may be more than one records available. Now what we can do we can iterate a loop. So here I can write contact on on list and I can write system dot debug. So I just want to display these details. So let's say I want to display contact name. So I can write on dot name. And if I want to display related account name so I can write account name plus con dot. So this way you need to fetch. So let's say we retrieved five records. So five times contact name and account name will be displayed. Right. So maybe what we can do we can put them together as well. So I'm cutting it here from here putting it here so that for one contact one account name will be displayed and so on. So now I'm going to execute this. So debug log will be opened and you will see the results. So here you can see contact name is Sanjay Gupta. So account is not associated with that final for beginning three then fourth one is showing a name as Sanjay Gupta and account name is tech school seven fifth one is having null. So account name is null. Then six one is contact name Sanjay Gupta and account name is Sanjay Gupta text. So this way you can apply the process. So after fetching the list you can iterate it in the loop and then you can fetch the details like this. And if you want, you can put this query here as well. Like this and you can remove this. Right. Now, here you can see we used account directly. But now if I applied this on custom object like child custom will be wearing parent custom value. So that time we need to use underscore underscore R. So I'm using book underscore underscore C be select ID name. And here I'm writing student underscore underscore R dot name. And from book underscore underscore C we are name equals to so book name I am putting B dash 05. So here you can see with the 005 students Sanjay is related. So now, first we are going to display book name through the dot name, and then student name through the dot. Student underscore underscore R dot name. Now I'm going to execute this. So you will see first book name will be displayed and the student is related with that book record so that student name will be displayed. Right. And if you want to give some space in between. So here you can write this. So now if I execute this so you will see some space between label and the value so that you can easily read. So this is book name so everyone spaces available and student name Aaron spaces available. So this way, we can fetch custom objects from child to paint. So remember one thing whenever you are working on parent to child or child to parent on custom object. So you need to use this underscore underscore R. So why parent to child so we need to use underscore underscore R with child relationship name and from child to parent so in that case you need to use this underscore underscore R instead of underscore underscore C. So I hope this way you understood all these things like how we can insert related records, how we can update related records and how we can query related records, both for standard and custom objects from parent to child and child to parent. Right. So I hope you understood whatever I demonstrated in this video. If you want to watch more Salesforce related videos so you can open this website study Salesforce.com and you will have lots of video collection there so do watch them. Thank you for watching this video.