 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to demonstrate a scenario with the help of Apex trigger and test class. Let's understand the requirement of trigger scenario. So in my previous video, I demonstrated you if we update account phone, then how we can update or populate that account phone field value on the related context home phone field. So I demonstrated that scenario with the help of map. So I implemented map in the business logic. Now we can implement that code with the help of Pay In Child SOQL as well. So this is another way to implement that problem. So you might be thinking which one is better. So always use of map is better, but you should know how we can apply Pay In Child SOQL as well in our Apex phone. So that is why I'm creating separate video for that purpose. So I will be implementing trigger and trigger handler and will be showing you the implementation of test class as well. So let's jump here. So in my previous video, I created this trigger and in this trigger dot is after this statement I added, which is for update related context. So here trigger dot new and trigger dot old map is passed. So once we update any report, so it will be having two different state. One state will be having the new value of the updated field and another will be having the old value of the updated field. So the new one will be available in this trigger dot new, which is a list. And old state will be available in this trigger dot old map, which is a map. Now here in this account trigger handler, I implemented the code. So here you can see update related contact method is available. So trigger dot new will be received in this ACC list and trigger dot old map will be received in this old ACC map. So here I'm going to comment this map statement. So I won't be deleting the code. I will be commenting and then like I will be implementing parent to child as so that you can see the differences. Right. So here instead of map, I will be using the set to create collection of IDs. So I'm creating set of ID. ACC IDs equals to new set of ID. Right. Now here instead of using map, I will be using set. So ACC IDs dot add ACC dot ID. And here if you see this loop, so we are iterating on ACC list. So ACC list will be having all the updated account records. So one by one, we are processing each account record. If account phone is not null and account phone is not equals to this old ACC map dot get. So ACC dot phone will give you the new value of phone field and this old map dot get. Then we are passing account ID as a key and we are fetching the phone field value. So this will give you the old value of that account phone field. So if you are updating any account records, so it will be having two values for phone field. One is new value and one is old value. So this selected part will give you the old value. And if both are not equal, it means only phone field is updated or maybe along with phone field you updated other fields as well. But phone field is anyhow updated and then only we just need to like perform this operation. And if old ACC map is not null, it means update is done. Then all the account IDs will be added into this set. Now here instead of map, we just need to use set to check the size. If set is blank, we don't need to process anything because no account is available in this set. It means no account is updated as per this condition. Account record updated, but they are not fulfilling this criteria. Now here you can see in this loop, I applied SOQL and here is only one loop, right? So instead of updating this, I'm just going to rebuild it. So I'm going to comment it out so that it will be available. And after this, I'm going to write new code. So this code is using map. Now I'm going to write code using parent to child SOQL. So here you will see multiple loops will be there. So sometimes if you need parent to child SOQL, so you can go with that as well. But if you want to reduce complexity or number of loops, then map will be the best option. So here I'm going to apply a loop for account ACC. And now I'm going to apply a SOQL. So select ID, phone, and then child query. So select home phone from contacts. So this context is child relationship name. Then where, sorry, from accounts. So this is parent account, parent object API account. Then where ID and ACC IDs. So I'm trying to query all the accounts whose ID is in this ACC ID set along with related contacts, right? Now this is basically parent to child SOQL. And here I'm going to write if ACC.context not equals to null. So we query child record. So if before understanding this, you need to understand parent to child SOQL as well. So you can go to APICS playlist. So there I explained parent to child SOQL in detail so you can understand from there. So basically when we apply parent to child SOQL, so we need to use this child relationship name to reference related contacts. So account ACC, parent account will be available in ACC. And all the related contacts will be available in this context. If it is not null, it means this particular account is having related contacts. So now what we can do, we can apply one more loop. So this is the drawback of this implementation. We need two loops here, right? So instead of implementing parent to child SOQL, it will be better to have a map implementation, right? So in this map implementation, we can just simply see like whether map is containing that particular key. If it is containing, then we are just assigning it directly. But here with the help of loop, we need to implement this operation. So here con dot home phone equals to ACC dot phone. And then we can add this con into on list so that it will be updated. So this way with the help of these two loops for each account, all the related contacts for home phone will be updated, right? And the rest of the code will remain same. So if I go here and do a refresh. So again, I will be updating the same account which I updated in the previous video. So I'm just opening this account record moving to details. So I just updated this from 1001. I'm updating it to two. So in the end, instead of one, I'm just replacing it with two and I'm clicking on save. If I go to related and open related contact. So here you can see home phone is updated. It is having two at the end. If I go back open another contact. So here also you can see value is updated. Right. So this way this is another way to implement the solution for this problem. But again, I'm saying the implementation of map is better as compared to parent to child. But you need to know both the ways. So that is why I'm implementing this video so that I can implement same scenario with a different approach. Now test class will be same. Because in test class, we just implement the reports and we we just perform insert update operations. So in your actual business logic, no matter you use map or parent to child as a kill your test class will remain same. So here are two lists are available. Then I created five accounts inserted them. Then it rated on inserted account list then created related context inserted context. Then I updated all accounts all five account records so that bulk update will also be tested. And then I queried one contact related to particular account. Right. And if that contact is updated with this latest phone field. So in that case, this trigger is working fine. Right. So I'm just going to run it again because we updated the code. So you can see all tests are passing. Nothing is failing. So it means same test method is working fine for the implementation that we did with the parent to child SOPL. Right. So there are two business logics, but one common test method will work fine for both the scenarios. So this is all about the scenario. So I just demonstrated the same scenario with parent to child SOPL. If you have not watched the previous one, so just go through the previous video where I just demonstrated the same scenario with the help of map.