 Hello everyone, I am Sanjay Gupta, I welcome you on Sanjay Gupta Tech School. In this video I am going to discuss about a trigger scenario and this trigger scenario I will be implementing along with its test class. So the requirement is when account billing address is updated then we need to populate the account billing address on all related context mailing address. So this is the requirement and I will be implementing this requirement using two ways. First I will demo you how you can implement the solution of this problem with the help of MAP and then I will be removing MAP and will show you how you can solve it with the help of parent child SOQL. So I will be implementing trigger and its trigger handler class and then we will see like how we can implement the test class. So jumping on to developer console, so I am going to open account trigger. So for my previous demonstration I already implemented account trigger and its trigger handler as well. So I am going to open account trigger. So here account trigger is available and you can see various methods are already called here because these I already demoed in previous videos. So here I am going to call one method. So we are talking about update. So on update of account we need to update related context. So here in this update part and in is after part I will be calling a method. So I am just going to copy paste this statement and naming it as update context mailing address. So this is my method name so now I am going to open the trigger handler and inside this trigger handler you can see various methods are already implemented. So at the end I am going to define this method. So public static void and then method name and it will be receiving two parameters. So from here you can see we are passing trigger dot new and trigger dot old map. So trigger dot new basically stores the new state of the records and trigger dot old map stores the old state of the record. Old state means old values of that updated record. So here they will be stored into different parameters. So I am just copying them from previous method I am going to paste it here. So this way this block is implemented I am just saving this code. So once it is saved you can easily save your trigger as well. So in trigger I just implemented this statement and also like if you are just implementing this example so remember to include after update here right and if you are following all the videos so I think you also have all these codes already implemented so you just need to add this method call here and then you just need to define it here okay. So now here I am going to create some code so if I take you above so here what I did I just created a map right so that map I am going to use here so I am just copying this code and I am going to paste it here. So first I am going to demo you like how we can solve this problem with the help of map. So I created a map of ID, account so ID will be the key and account will be storing the values so values are of type account and the key are of type ID right and the name of map is ACC to account map right. Now what I need to do I just need to implement a loop so that I can iterate on ACC list so for account ACC colon ACC list and inside this loop I am going to check each account billing fields whether they are like updated or not so if I take you to the account object and if I try to search for billing address so you will find this complete field altogether but addresses are basically divided into some subfields so if I take you to account and open any of the account record and if we go to details so here if I added it so under address you can see we have billing street, billing city, state, zip code and country so these five fields are available and you can update any anyone whenever you update any of these fields so your trigger should work. So we just need to apply this condition here so we need to compare old value of particular field with new value so new values are available under ACC because we are iterating through ACC in the loop so ACC dot billing city right so it will give you the new value then not equals to and here I need to write old map dot get ACC dot id and dot billing city right so this this part is basically for comparing new value of billing city and old value of billing city that is available for a particular record and that record is available in this ACC variable so now this way we need to check it for other fields as well so I am just applying that so I am using our condition here and I am going to use a bracket here and a bracket here so everything will be inside this column bracket right so now what I need to do I just need to copy it and I am pasting it then again or sign pasting it then again or sign pasting it or and then paste right so five times it is available now instead of billing city dot billing country so here also I just need to paste billing country the next is billing postal code and remember I just used or like if anyone is changing so this trigger will work so billing state and then last one is billing street right so anyone of these conditions are true so this condition will be true after this bracket I am just putting and and I am going to check old map not equals to null right so we just wanted to run on update only so on update your old map will be having some values it won't be null right so this way it is compared now inside this if condition I just need to write the code so I am going to use this map so inside this map I just need to put the value so map dot put then ACC dot ID will become the key and ACC will become the value so there are some errors so let's see them variable does not exist old map okay so here variable name is old ACC map so I am just replacing it to old map right so now this first part is completed like we rated on this ACC list compared whether a particular field is updated or not if it is updated then that particular record is like storing into this map so that we can use it later on now after this implementation I just need to implement one more for loop and I am going to query context so here I am going to write SOQL select ID comma account ID from contact where account ID in now I am going to use that map so map name dot key set so key set is a method which returns all the keys those are available inside map so basis on that keys whose account ID like on contact we have a lookup that is account ID so if context account ID lookup is having some value and it is matching with this key so it means that contact will be queried right so now inside this for loop I am going to check if that map contains key and we need to pass con dot account ID so right now we just queried one contact so if that contact dot account ID is available in this map right so it means that contact that we are querying its account its parent account is available in the map it means that accounts billing address is updated right so what we need to do we just need to copy those billing information into mailing information right so here what I am going to do I am going to write con dot mailing city equals to then map dot get con dot account ID and dot billing city so this way you just need to copy so this is straightforward con dot mailing city so it is belonging to this variable and through map we are getting this we are just passing this key con dot account ID if this key is available so get function will be returning the value associated and from that value we are just picking the billing city part now I am just going to copy and paste it five times and I just need to replace these so billing city now billing country then postal code then state and last one is street so this way I just modified this code so now accounts billing information will copy to context mailing information right now what we can do we just need to create a list of contact and in this con list we just need to add this contact right so this way our for loop is implemented and now after completion of this for loop you can just apply if condition and you can just check this con list for empty so if it is not empty then what you can do just update your playlist so whatever records are available under this con list so they will be updated with the help of this code so this way I use map and you can see through a single loop we are able to modify the mailing information of contact so we are just querying the context which are associated with particular account and that account ID is available in this maps key set right so this way I demoed you like how we can implement the solution with the help of map so now I am just saving this code and we will demo you from the UI so I am going to just check whether this is having a related contact yes this account is having a related contact so I am moving to details and from here I am going to update this so billing city instead of japper let's say I am entering Mumbai other things are same so I am just saving it and if I go to related list and open this contact a new tab so I just need to check the mailing information on this record so if I scroll down so you can see mailing address is updated and it is having city as Mumbai and other things are also available right so this way a code is working fine now I am going to modify this code with the help of parent to child SOQL so that you can understand a different way as well so to implement parent to child SOQL I am just commenting this map and also remember like when I will be implementing the test class so test class will be same for both the functionalities because in test class we just need to create the records and we just need to update the record so that this trigger will fire so there may be different ways to implement the code but test class will be same in most of the cases so now I am just creating a set of ID naming it as ACC ID new set of ID right now after this same iteration will be required and same conditions instead of map here I just need to use set so I am just adding like ACC IDs dot add and here I am writing ACC dot ID so instead of putting values into map I am just adding IDs into this set right now we just need to apply parent to child SOQL so I am just commenting this code and here I am going to write new code so I am applying for account so if we apply parent to child SOQL so we will be applying the query on account and through account all the related context we will be querying so select ID then we need to query all the fields so billing country billing city billing postal code billing state and billing street then comma and we need to write inner query so select ID from context now after this from account where ID in and then the set which we created so all the account records will be queried whose ID is available in this set along with the related context right so now we can apply if condition so if ACC dot context not equals to null so what does it mean it means like if you are querying any account and if it is not having any related context so it will be like this context is child relationship name so if there is no contact associated with any account so it will be null so if ACC dot context is not null it means there are some contacts and we just need to modify them so we can apply one more loop so loop will be trading on ACC dot context and one by one contact will be available into con variable so what we need to do we just need to write con dot mailing city equals to ACC dot billing city so like this con dot mailing country equals to ACC dot billing country then con dot mailing postal code equals to ACC dot billing postal code on dot mailing state state equals to ACC dot billing state con dot mailing street equals to ACC dot billing street so this way we copied all the information now we just need to add this contact into con list right and rest of the part will be same so here this for loop is closing here this if condition is closing and then here outer for loop is closing so if you want to apply parent to child as well so the drawback is you need to implement nested loops so always use of map will be the best because here through single loop you are able to implement the solution so here you can see different logic is implemented and rest of the code will remain same right so now I'm going to show you how you can implement its test class so I'm going to open account trigger handler test and I'm going to implement the test method so lots of methods are already implemented here so we can take help from here as well so what I'm going to do I'm going to copy this method and I'm going to paste it and we need to test related contact mailing address right so here we need to create sample data so account list and contact list both are declared here then through this loop I'm trying to create accounts right so here I just need to fill all the information so so billing city so let's say test city then acc dot billing country equals to test country acc dot billing postal code equals to some random postal code acc dot state equals to test state acc dot billing equals to billing street equals to test street so this way account five account records will be created along with this information then we just inserted them after insertion here you can see one more loop is available which is creating related contact and we are just creating contact and assigning account id into account id lookup so that the newly created contact will be linked with particular account so each account will be having one contact associated so contacts are inserted now here we are iterating on account list so here instead of phone I am just going to modify billing city so earlier you can see billing city is test city now here I am going to write it as Mumbai right so initially all the records will be having test city but for all the records I just updated billing city so it means all the account records related contact should be updated and will be having updated information right so what I need to do I just need to query mailing city from contact and I am just fetching the first contact that we created through this con list here right if first contacts mailing city is equals to Mumbai it means code is working fine so here instead of home phone we just need to compare mailing city so I am just saving this code so test class is saved right now I am going to run this test class so I am just clicking on run test and after this I just need to move this test tab so that I can see whether it is failing or passing so all tests are passed and if I go here and check the code cover is so it is 100% all the trigger statements are covered and if I go here so 79% code is covered and we just need to focus on the method we just implemented and you can see all the lines are covered why it is 79% because some of the code is not working maybe I just commented the statement so here you can see two statements are commented like these two methods are not calling so that is why code cover is less but the method which we implemented right now it is covered completely so this way I hope you understood like how we can implement a scenario through map as well as through parent to child SOQL so I would prefer as part of best practice always try to use maps so that you can reduce loop complexity right and I hope you understood how we can just implement the test class so in this video I just demoed you like how you can copy previous test methods and you can modify it as per your requirement thank you