 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. StudySalesforce.com is my website, a video library to learn Salesforce. In this scenario, I'm going to demonstrate how you can query or how you can apply SOQL from parent to child. So in my previous video, I demonstrated you parent to child SOQL where lookup field was standard. But here in this video, we will be seeing like if you have created a lookup field on any object as custom, so how you can apply parent to child SOQL and what things you need to take care while applying parent to child SOQL in case of a custom lookup relationship. So what we need to do, we need to create a custom object named employee. It can be standard as well, but your lookup relationship should be custom. So we need to create salary and lookup to account fields on employee object. Then we need to create a total salary field on account. Then we need to query accounts created this year along with related employees. And we need to populate total salary of all related employees on account. So this is the use case. So let's first create or check whether these fields are available or not. So I'm jumping to employee first. So in my org employee record, sorry, employee object is already created. If in your org, you don't have employee so you can create or maybe you can create this custom lookup relationship on any standard object as well. But make sure this should be custom. So in my org on employee object, I have already created lookup to account. And if I open this, so here you can see all the information. It is a custom field created by me, right? And here's the field name that we will be using to fetch fields related to account. And if we apply a parent to child SOQL, so in that case, we need to use child relationship name that is available here, right? So basically this field name we use when we apply child to parent. But in this video, I'm going to demonstrate how you can query data from parent to child. So in this case, we will be focusing on this child relationship name, right? And let me see whether salary field is available or not. So it is not available. So I'm going to create that field. So I am selecting currency, naming it as salary, visible to all profiles, visible to layout as well, clicking on save. So here you can see salary field is also created. Now I'm moving to account and on account, there is no total salary field available. So I'm going to create that. So in this total salary, all the related employees, total salary will be available. So this way, all basic requirement is complete. So if I take you back to the use case, so a custom object is available, salary and lookup to account fields are available. Total salary field is available on account. Now we need to apply SOQL. So let me take you to developer console and let's create SOQL first. Then we will be using that SOQL in the apex code. So I need to apply SOQL on account. So I will be fetching account ID, total salary that we created. Now inside parenthesis, I need to write child query. So select ID, salary underscore underscore C. So remember if a field is custom, so you need to use this underscore underscore C. Then from and after this from, if you have watched my video on parent to child SOQL, where standard relationship or you can say standard lookup field is available. So in that video, I showed you like we need to use child relationship name here. So again, I'm going to show you. So child relationship name always exist where you created lookup field. So we create lookup field on child object always. So here you can see this is the child relationship name that I need to use. But in case of custom, you need to attach underscore underscore R as well. So this is the basic difference. So if you have standard relationship field, then you will be using child relationship name as is. But if that relationship field is custom, so you need to use underscore underscore R as well. And then from account and then we can apply where we are clause. So we are created date equals to this year and click on execute. So here you can see all accounts are query their total salary are blank. And here in this account record one employee is related and they are also salaries null. So let's take a look on some accounts. Or maybe I can open the employer and on this employer this account is related. Right. So I'm going to fill the salary field. So I'm doing a refresh because that field is not available. Now it is available. So I'm putting 10,000. I'm going and creating one most employee Davis and creating for same so that we can see the total count. To and one I'm going to create for any other account. So three employer records are available. So if I again execute this as a cure. So you will see the changes. So I'm clicking on execute. So here you can see. On these two accounts. We have employed permission. Right. So now I'm going to write the code so that you can understand how. Child. So you can be implemented in the effects. Now I will copy this. Complete. And pasting it here. Splitting it into different lines so that we can. Read it properly. Now I need to check it for now. So if this list is not empty. So I'm writing a loop. So count. ACC. Then this list name. So one by one all account. The courts will be available in this ACC instance. Now. Or through this ACC. I can have this total salary updated. Right. So. I'm creating. One more variable. As total salary initializing it with zero. Now we need to iterate. One more. Because we have child relationship. Information under this employees underscore underscore R. So here I'm putting employee underscore underscore C EMP. ACC dot. Employees underscore underscore R. So if you have. Child records through this. Take it to child SOQL. So if you want to iterate a loop on those child records. So this way you can do that. So we need to. Put the object API. Then instance name. Then through ACC dot. And then employees underscore underscore R. So it will be having all child related information. So now what I can do. Total. Equals to total salary plus. EMP dot. Salary underscore underscore C. Right. So. All. Employees. Salary will be added to this total salary. Now after completion of this loop. I can write. And then employees underscore underscore R. So it will be having all child related information. So now what I can do. Total. Salary equals to total salary plus. Total salary equals to. Total. Salary. So this. It will be added. To the account. And. After completion of this. If. We can have another. So I'm copying this. The. And here I can write. Update. This is. This is. This is. This is. This is. This. Update. Debut. Okay. Equal. From Desections. So we need to convert. December. Because that you can see her decimal. So we need to lose data by decimal. So here you can see Accord. So now we can save the score. I'm going to execute this code. So copying this method name. I'll take it here. Clicking large. Now, moving here, and again I'm going to run this SOQL, so you will see total. So other than these two, all are having zero, because they don't have any rapid employee, but where related employee records are available, there you can see total salary is available. So here total will be 10,000 plus 20,000, so 30,000 is available. Here we have only one contact, sorry one employee. So 20,000 is available. So this way, if you want to apply a payment to giant SOQ value, where you have created custom lookup relationship fee, so this way I hope you understood how you can manage that thing. So with the help of this use case, I tried to demonstrate how we can apply a payment to SOQ on custom lookup relationship. So implement this in your org so that you can understand and studysalesforce.com is my website. Here, all the Salesforce related video links are available. Thank you for watching this video.