 Hello everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. StudySalesForce.com is my website or video library to learn Salesforce. In this video, I'm going to demonstrate one scenario. So here I will be querying accounts created in last week. I need to populate total contacts like which are associated with accounts. So I will be creating this field total contacts. So we need to count number of contacts which are associated with account. If there is no contact associated, then we need to populate zero. So with this demonstration, you will get to know how we can apply parent to child query in Apex. So let's jump to org. So first I'm going to create that field. So simply I'm opening account. So now here I need to click on fields and relationships. So let me search for total contacts. No field is available. So I'm going to create a net new field. Yep. So it is available. Now I'm clicking on new. Selecting for number field. Clicking on next. So I'm labeling it as total contacts. Clicking on next visible to all the profiles clicking on next will be available on all the page layouts. So this way I created the field. Now I'm moving to developer console. Right. So let's first implement pay into child query. Right. So right now both are standard objects. Account and contact and account and contact both share a lookup field. So if I take you there. So I need to go to contact now. So searching for contacts. So in context fields and relationships, here you can see this account ID field is available, which is look up to account. Right. If I click and open this. So here you will find this child relationship name. So if you read this help text, so it says this field defines a relationship between this object type and a parent object type. Right. So so the child relationship name is used in SOPL queries on the parent object type to refer to this object type. For example, contact has a lookup field for account. The child relationship name for the lookup field on contact is contacts. Therefore, you would use contacts in account queries to refer to contact records whose account field references a given account, accepting the default value safe. If necessary, the name can be changed later. Right. So if you want to query all the context, those are associated with the account altogether with a single query single SQL. In that case, you need to use this child relationship name. So though this is a standard field, you don't need to create this child relationship name. Whenever you will be creating a custom field, then it will be created like at the time of creation of that particular lookup field. So now I'm going to use this here. So select ID name. So you need to use this bracket. Then again, select ID, let's say first name, last name, then from and here you need to write contacts. So instead of contact, you need to write contacts, then from account where created date equals to last week. So this way, and these are like capital letters I'm using for best practice. So if you write them in small letters, so they also works in that case as well. Right. So if I click on execute, yeah, it is giving an error. So yeah, I just forgot to put a comma here. So, clicking on execute. So here you can see it is not showing anything. But we are created date equals to last week. So maybe this week. No. So I'm taking last month. Yeah, we have various records. So here you can see account IDs then account names and then you can see contacts. So these are contact IDs then first name and then last. So this way all the data we are able to fetch now using this SOQL I can perform my operation that I want to do. So here I'm going to create a method public static void count contacts. Now I'm creating list of account ACC list equals to and then I'm pasting the square. Right. So, okay, so this query is available. I'm just splitting it so that we can leave it carefully or properly. So select ID name. These are fields of account. And this is basically parent to child query. So this is important if you want to apply parent to child query. So pay into child SOQL. Right. And this is child relationship name. So I already told you like from where you can find this. So it is available. Now what we need to do after implementing this SOQL. So all the data will be available into this account list. Now we can apply if condition. So if not ACC list dot is empty. If it is not empty, then I can iterate a loop over here. So account ACC then ACC list. Now inside this loop, I need to implement the logic so that I can update that total count field. Right. So, okay, I need to use columns in here. Now ACC dot total count underscore underscore, sorry, total context, not count equals to ACC dot context dot count. So this way you need to use this method. So ACC is the instance which will be having each account. Then all the context you can pass through through this context. Now if you want to count this list that is context, so you can simply use count. Right. It is showing some error. Let's see whether we are able to use size. Yes, we are able to. So instead of count, you need to use size. Right. So if you see some error, so this way you can test other methods as well. So after implementation of this, so you just need to check null again. So ACC list dot is empty. And then you can write update ACC list. Right. So this way, everything is available here. I'm just saving this code so that I can execute it. So this time, what I'm going to do, I'm just adding count contacts underscore underscore C as well and I'm clicking on execute. Okay, total context. So here you can see it is blank as of now. Right. Now I'm going to execute my code. So you will see the effects. So name of method is count contacts. Clicking on execute. Again, I'm going to execute this SOQM. So here you can see this field is updated on account with respect to the number of contacts associated with that account. So here you can see no contact is available. So it is zero. Here we have one. So one, if there are two contacts or two. So this way all the account records are updated automatically. So this way, I guess you understood how we can apply parent to child SOQL if we have that lookup field as standard. Right. If we have that lookup field as custom. So in that case, we need to do like we need to do different things with this child relationship name. So that I'm going to demonstrate in my another video. So this is basically parent to child SOQL on standard objects and the standard field that is lookup to account. So I hope you understood whatever I demonstrated here. If you if you go here and search for particular account. Let's say, yeah, if we open any account, so you will see that total contacts field available here and it will be having the value populated. So both from UI, you can check or through the packet through SOQL you can also test whether your updation performed or not. So this is all about the demonstration related to this scenario. If you want to watch more Salesforce related videos so you can visit study salesforce.com. Thank you for watching this video.