 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 video, I'm going to implement a scenario. So I will be querying accounts created last week. And I need to populate account phone on related context. So I will show you this implementation through two ways. One is parent to child SOQL. And another is using map. So by watching this video, you will get understanding like how we can implement map in Apex. So jumping to developer console. So let's let's first implement it using parent to child SOQL. So if I create parent to child SOQL, so I need to write these things. So what I did, I just wrote select ID phone. So these are account fields. Then inside parenthesis, this is child query. So together it is known as parent to child SOQL. And here this context is basically child relationship name, which you can find through lookup field. So if I take you to contact, so on contact we have a lookup to account. So if you open that lookup. So there you can find the child relationship name here. Right. So you can watch my another videos as well. Those particularly are made for parent to child SOQL. So we can apply parent to child SOQL on both like standard relationship and custom relationship. So there you can understand that in detail. So it is available in this playlist only. So with this SOQL all accounts and related contacts will be queried. Those are created last week. So clicking on execute. So here you can see the records are available. So account ID and account phones, phone numbers are available. So we need to apply one check as well. If account is having phone populated, then only we need to copy it on account. Sorry, on contacts, right? So if we can, we can apply the condition by iterating this loop, right? So here you can also notice for one account contact is available for others related contact is not available. So if contact is available, then phone number will be copied otherwise not. So now what I need to do, I need to copy this SOQL and here I am creating a list of account and pasting this, right? So this way SOQL is available. Now I need to check this ACC list for null. If it is not empty, then I can iterate a loop and here I can check if ACC dot phone not equals to null. So you can apply this condition in this we are close as well like we have created it equals to last week and phone not equals to null. So only those records will be queried. And if you don't apply that here in this SOQL, so you can apply that here as well. So phone phone is not null. So what you can do, you can iterate one more loop contacts con colon then ACC dot contacts. So one by one contacts will be available here. Now you can write con dot phone equals to ACC dot phone. And now here we need to create one more list list of contact con list equals to new list of contact. And here contact con equals to new contact con dot ID equals to so con record. Con record equals to con dot ID and con record phone equals to this. Now we can add this con record into con list. So this way this contact list will be having all the contacts. Those we are going to update. Now after completion of this if block I can apply if not con list dot is empty. If it is not empty, then I can write update con list. Now I'm saving this moving here moving to accounts, opening accounts created last week and opening this one so that I can have related contact. So I'm creating new contact using the phone number saved it so phone is blank. So on account phone number is available but on contact phone number is not available. So let's apply that as a kill once again. So here you can see for this account for them for this contact phone is not. So now I will be executing the code which I implemented. So you will see this account phone will be populated on this contact phone. So I'm closing this. So I'm going to execute this. Again, I'm going to run this query. And here you can see phone is updated. Right. So this is the one way. Now I'm going to show you another way. So for that, I am removing this. So what we are going to do. We are just going to query accounts in this ACC list. I implemented parent to child SQL already. Now I'm focusing on implementation using map. So now you will see like how we can use map in the apex. So list of account ACC list. Select ID phone from account where created date equals to last week. Right. Now I need to assign this list into a map. So I'm creating a map of ID. So ACC ID to. Count map equals to new map of ID, account. Now if you pass a list into a map. So automatically IDs and that account will be managed. Right. Also, you can do one thing like you can have a loop on ACC list. And what you can do, you can use this map. Put ACC dot ID and ACC. So this also you can do. So I'm commenting this for future reference. Right. So if you want to assign map in this way, so it is also possible. So list will be converted into map. And this is also possible. Now I need to query all the context. So first we read all the accounts. And then converted account list into map. Now with the help of this map, I will be querying list of contact. So, so con list equals to select ID. Account ID phone. On contact. We're. Account ID in. Then you need to use this map. Dot key set. So if we apply key set method with map, so it returns all the keys, those are available in the map and basis on those keys. This condition will be matched like account ID in means available in this key set and those contacts will be queried. Right. So this way we can use map to query the records. So one thing I am going to implement here as well. And phone not equals to null. So I'm applying this check here only so that only those records, those account records will be queried. We are phone is not null. Now here. The naming it as context to be updated. Now here I need to write con list. So if con list is not empty. So I'm going to iterate on contact list. So earlier we iterated on account list, but now we are iterating on contact list. Right. So. In this contact list. Now I need to apply if. So what we are going to do, we are using this map. Dot. Contains key. So we need to check whether this. Contacts related account is available in this map or not. So I'm writing this. So what I did, I use this map. Use this method contains key with check whether particular keys available in the map or not. So key we are passing as con dot account ID. So for this contact, if it's related account is available in this map, then only we need to apply some operation. And what we need to do, we need to copy accounts phone into this contact. Right. So now. We can create. Contact instance con record equals to new. Contact. Con record dot ID equals to con dot ID. Con record dot. Phone equals to. Then you need to use this map. Dot get. So if you want to get the value associated with a key. Through map. So you need to use this method get. So I'm passing con dot account ID as a key. So corresponding value is basically account record. And I want to fetch phone field only. So I'm writing phone. So what will happen through this map. Through this cat method. I'm passing this key. So basis on this key, the value which is associated with the key. That is account record and that account records phone. I'm fetching and assigning it to related contact. Right. So this way I'm mapping it. And now. I'm copying this list. Dot add. Con record. Now. Copying this putting it here and here. So this way I used map in this example. So this example can be implemented with both the. The. Through parent to child SOPL or through this way as well using map. Now. I need to. View last week created account. So I'm opening this. So here also. Related contact is not available. So what I'm going to do. Test one to three. Test three to one. And. Saving this. So phone is not available. So again, before executing this method, I am applying this. Pay in to child SOPL. So remember. Pay in to child SOPL. I'm using to check. Whether. Phone number is available in the related contact or not. But in actual code, we are not using parent to child SOPL. So after executing this method. Again, we will run this parent to child SOPL and we'll see like. Account phone will be updated into contact phone. So. Executing this. So method name is same update contact phone. Now clicking on execute. It is executed. So here you can see. All contacts are having the phone. So one was not having a phone number earlier. It was null. Now it is having the phone number, which is available on account. So this way I demonstrated you like how you can implement. Code in two different ways. Using parent to child SOQL as well as using map. So I hope you understood this demonstration. If you want to watch more Salesforce related videos. So visit study Salesforce.com. Complete video library to learn Salesforce. Thank you for watching this video.