 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to discuss about a scenario based on trigger and test class. So let's understand the trigger scenario that I'm going to implement along with its test class. So the requirement of the scenario is when account phone is updated, then we need to populate accounts phone number on all related contacts in home phone field. So in short, if account record phone is updated, so you just need to update all the related contacts. And on contact we have variety of phone fields, so we just need to update home phone field. And I will be demonstrating this scenario with the help of map. So we can implement this scenario with two ways like either we can use map or we can use parent to child SOQL. So in this video, I will be using map and in another upcoming video, I will be demonstrating the same scenario with the help of parent child SOQL. So we need to implement trigger as well as trigger handler. And also I will be writing the test class for code coverage. So as you can see, we need to update the account. So the trigger will be implemented on account. So I'm jumping to developer console. I'm going to open the resources. So on account, I already implemented a trigger because I already demoed many of the examples. So here you can see some of the functions are called like update rating, copy billing to shipping, create contact or opportunity. So these are for insert. Then we have two options like is before is after. Now here we need to apply this trigger on update because we are updating the account. So we need to implement the trigger dot is update part. Now we need to decide whether to go for before or after. So we will be updating account and then we need to update the related context. So after update will be better because we will be working on the related record. So here first of all, I'm just adding after update event. So now this trigger is having four events before insert after insert before update and after update. Now here I'm writing else if and then trigger dot is after and then its block will be available. So I'm just copying the statement. I'm going to paste it here and method name will be update related contacts. And then I will be passing trigger dot new and trigger dot old map. So trigger dot new will be having the new state of the reports. Those are updated and trigger dot old map will be having the old state of those reports which are updated. So the same record will be available in trigger dot new as well as in trigger dot old map. And as in previous videos I already explained like this trigger dot new is basically a list and trigger dot old map is basically map which will be having key and value pair. Now I'm going to open account trigger handler where I will be implementing the business logic. So here you can see lots of methods are already implemented. So now I'm going to implement one more method inside this. So I'm just going to copy this name and here I'm going to implement it. So public static void and then method name and here I just need to write this. So we are passing trigger dot new and trigger dot old map. 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 this way I'm saving this code so that here we will be able to save the trigger because it is showing error. Now it is saved successfully. Now we need to focus on the implementation of this part. So I'm going to implement this method so that you can understand the business logic like how if account is updated like if accounts phone is updated. So how we can update the related content and we will be implementing this method for bulk account records like if you update bulk accounts through data loader or some other tool. So in that case also your code should work. So that is why we are just receiving list of account in this ACC list which will be having new state and old state will be available in this old ACC map. Right. So if you update more than one account record so the logic will work for those records properly. So now I'm going to create list of contact and then I'm going to create a map. So map of ID comma accounts ID will be key and account will be value. So I'm naming it as ACC to account map then equals to new map of ID comma account. So this way I created two variable one is of type list and one is of type map. Now I'm going to iterate on this ACC list. So ACC list is containing all the account records which are updated and this ACC list will be having the new state. Now I'm going to check ACC dot phone if it is not equals to null. So if account phone is not equals to null then only we wanted to update and apart from this condition we also need to check whether account record is updated due to phone field updation. Right. If it is updated due to any other field updation in that case we don't need to run our code. Right. So here I'm going to check ACC dot phone. So ACC will be having values from this ACC list and ACC list will be having values from trigger dot new. So trigger dot new will be passing the new state of the record new state of the updated record. So you can assume like this ACC dot phone is having the new value of that updated record phone value and the previous value which was already there on that record. So that you can receive with the help of old map. So what you can do you can just write old map sorry old ACC map dot get and then you can pass ACC dot ID and then you can have dot phone. So this way, like this is the name of the map. Then through map if you want to get the value so you just need to call the get method and to receive particular value you just need to pass the key. So this ACC dot ID will be the key and through this the value which we will be receiving so I just need phone field. And after that I am just adding one more and that will be for like null check. So I am applying old ACC map not equals to null. So if it is not null then only it will be performed. So anyhow if you don't write this then also this method is already called inside after update. So it means like if record is updated then anyhow old account map won't be null. But still if you want to write this condition you can now inside this if we can just fill this map. So if all these conditions are true so ACC to account map dot put then ACC dot ID and ACC. So if account phone is not null and account phone like new value is not equals to old value it means record is updated due to phone value. So in that case that account ID and account record will be added into the map with the help of put method. Right so all those account records will be available in this map. Now after this loop what we need to do we need to apply a for loop. Right so before applying this for loop what we can do we can apply a null check like if this map is blank. If there is no account record in that case we don't need to perform any other logic. So I'm just writing if then I'm copying this map and applying size greater than zero. So if this map size is greater than zero then only we need to implement the for the logic. So now what I'm going to do I'm going to query the related contacts. Right so I'm just writing for loop here. Contact con colon and then I'm going to write SOQL. So select ID home phone and account ID from contact where account ID in and then I'm just pasting the map dot key set. Right so this map is having ACC ID like account IDs in the key part. So if if I want to fetch all the keys so I can just use key set method. So this key set will be giving all the keys. Those keys are account record IDs and if those account record IDs like they if they matches with account ID field that is available on contact. So all those contacts will be added. Right so now inside this loop we need to implement the logic. So here I'm writing if condition then pasting the map. So if map dot contains key then con dot account ID. So what I'm going to do I'm going to check whether that map is containing the queried contacts account ID like if contact is linked with particular account. So that account ID should be available in this map. If it is available then what we can do con dot home phone equals to then that map dot get con dot account ID dot phone. So this way through that map we are getting the value basis on this key that we are passing as con dot account ID. And the return to values phone field we are just going to assign into this con dot home phone. So this way the particular contact which is available in this con variable is updated. Now I'm going to add that contact into this con list. Right so this way with the help of map we are trying to update phone phone field of the particular contact. So one by one each contact will be treated and that contact is particularly linked with account. So that account ID we are just searching in this map if it is available. So that contacts phone sorry that accounts phone we are just populating on like contact home phone. And then we are adding that contact to contact list. So this way this loop will be trading and after completion of this process I'm just applying if condition for con list. So if con list dot size is greater than zero then I can just write update on list. So this way the business logic is implemented right so I just saved this code. Now I'm jumping to the UI and I'm going to open account record. So I'm going to open account record where our contact is already related. So here you can see two contacts are available. Right and if I go to details I'm going to update the phone field value. So here you can see phone is having three one two five nine six one triple zero. So last I just updated zero to one and I'm going to click on save. Now I'm moving to related and if I open this contact. So I just need to check the home phone field. So here you can see home phone is having that updated value. Right. If I go back and open another contact. So if I go to details so you can see this context home phone field is also updated. So this way this code is working fine. So now we just need to implement its test class so that we can see like through test class whether this code is working fine or not. So I'm going to open the test class now. So in the test class we just need to create the data. So we need to create account records. We need to create contact records. So here I'm going to write the test class. So first of all at the latest test. Right. So here you can see various methods are already implemented to cover other functionalities. So what I'm going to do I'm going to copy some code from here. So I'm copying this piece and I'm going to paste it here. So update related contact us. Then we need to create sample data for accounts. So five account reports will be created. And here I'm going to populate phone field value as well. Okay. So five account records will be created and they will be added into a CC list. Then I can just write insert a CC list. Okay. Now if I go. So in your projects as well, if you have a test class already implemented, so you just need to search whether the data is already available there or not. So I'm just trying to search whether somewhere context are created or not. So I am unable to find that. So no problem. I'm going to create context. So I'm going to iterate on this account list. So account ACC then ACC list. So one by one each account will be available into this ACC. And before moving forward, I'm going to create a list of contact con list equals to a new list of contact. Now here I'm going to create new contact. And here last name is required. So I'm just going to populate account name as last name. And I just want to link this account with the. So I want to link this contact with account so on dot account ID equals to ACC dot ID. So this way this newly created contact will be linked to the account. Now I can just add this contact into this list, right? So this way each account will be having one related contact. Now after this I'm writing insert con list, right? And what you can do, you can just write start, sorry, test dot start test. And here you can write test dot stop test. So these two statements we use so that fresh governor limits will be available for this particular test method. Okay, so after inserting this con list now I'm going to update the account records. So I'm going to implement this loop again. And I'm going to update the phone values with 654321. Right, so once I will be updating the phone value, then I just need to query the related contacts. And if contact home phone is having the same value, it means our logic is working fine. So on this update ACC list trigger will be working automatically. So after this, I will be wearing contacts. So list of contact updated contact list equals to select ID home phone from contact where. Okay, so where ID, I'm just going to query only one record. So where ID equals to on list of zero dot ID, right? So first contact I'm just query, and I'm going to apply system dot assert equals now. So here I'm going to check expected and actual so expected is this and actual is available in this updated con list of zero dot home phone. Right, so we queried contact into this list. So in this list on 0th index that contact record will be available if its phone is equals to this value. So it means on account update related contacts home phone is updated automatically. So I'm just saving this. Now I'm just opening this and here test will be performed. So I'm just clicking on run test. So we just need to focus on this method. Update related contact test. And here you can see total nine methods executed because in this test class nine methods are available as I demonstrated lots of scenarios in the previous video so you can just find them. And here you can see update related contact test so it is it is available at bottom and it is passing. So it means our business logic is working fine as we implemented test class here. Right. So this way if you want to like test your apex code. So always remember one thing whenever you try to implement your test class. So you need to create data in bulk. And on bulk data you need to apply the operation if your code is working fine for bulk data. It means it will be working fine for single record as well. So here you can see like I updated all the account records which I created and for testing purpose I randomly picked one contact. Right. So if you want to check other contact records so you can just query them and you can apply a search equals. So that is also possible. So this way I hope you understood this scenario that I implemented with the help of trigger and test class and you saw like I just implemented this business logic with the help of map. So in my upcoming video the same scenario I will be implementing with the help of parent child as well. So there I won't be using map. I will be using parent child as well. So in that case, like we will be using nested off loop. So you have both the options. If you want to reduce complexity you can prefer map. And if you don't want to use map because map implementation is a little bit complicated. So if you want to go with parent child as well. So that is another option. So keep. So keep watching these videos because these are related to trigger scenario and test classes which will strengthen your apex trigger and test class skills. So this is all in this video. So in upcoming video I will be posting the same scenario with parent child as well. Thank you.