 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to demonstrate a scenario based on trigger and test class. Let's understand the scenario first. So scenario says when an account is inserted or updated and the copy billing to shipping checkbox is checked, then copy billing address to shipping address. So actually this insert pass part of this requirement I already demonstrated with one of the video. So what we are going to do, we will be understanding like how that insert method can be utilized or reused for update operation as well. So I am going to modify that so that this requirement can be accomplished for both insert and update operation. So we need account trigger, then trigger handler class and then test class as well. So I am jumping to developer console. So here you can see account trigger is already there and here is the method that I am calling for insert, before insert, copy billing to shipping. So I am just copying it and I am pasting it in before update section. So whenever we update any record, so we have two entities available. One is trigger.new, which is having new state of the record and another is trigger.oldmap, which will be having old state of the record. So the same updated record will be having, will be available in this trigger.new and this trigger.oldmap. So later on we can compare like if particular field is changed, then only we want to run that particular code. Now if you are using, reusing the same method, so here we are passing two parameters, so at least two parameters we need to pass from insert as well. So here second parameter we can pass as null because in case of insert, we don't have any trigger.oldmap. Right. Accordingly we need to modify the code here in the trigger handler. So here first of all I am writing map of id, account old map. So this acclist will be receiving trigger.new values and old map will be receiving the old map value in case of update and in case of insert it will be null because from here we are passing null. So this also will be deciding whether this code will be executing for insert operation or for update operation. Okay. So what we need to do, first we need to do insert thing. So I am just modifying this code and adding this piece. So here you can see this is insert part now like if old map is null it means insert is happening and if copy billing to shipping is true on insert then we just need to copy all the billing information into shipping information. Then for update we can have this OR part and then first of all we need to check whether this copy billing to shipping is true or not. If it is true then we can apply and so what I need to do, acc.copybillingtoshipping not equals to oldmap.get acc.id.this field copy billing to shipping. Okay. So this acc.copybilling to shipping it is having the new value of that field and oldmap will give the old value of that field if both are not matching it means that field is updated and if it is true so let me just have a bracket here as well and here as well. Okay. So this whole part is for update. So first we are checking whether this copy billing to shipping is true. If it is true then we are checking whether it became true from false or not. If yes then only it will work. Right. And the rest of the code will be same like we are just copying all the billing information to shipping information. Right. So this is complete. Now I am just saving this code so that I can demonstrate it from the UI. I am saving the trigger part as well. Now jumping to the UI and I am first going to create new account and I am checking this checkbox copy billing to shipping and here I am going to fill the information. So shipping part is blank, billing is filled and this checkbox is checked and we are inserting the record. So record is being created. If I go to details so here you can see information is copied. Now I am going to open any existing record where this copy billing to shipping is unchecked and billing information is not available. So I am just calculating it and I am checking this checkbox as well. Copy billing to shipping. Right. So this field is getting updated so that trigger will work, condition will work and it will copy all the billing information to shipping. So I am just saving it and here you can see information is copied automatically. So in both the cases code is working fine. Now after this we need to implement the test class. So test class is already available here and here you can see I have this copy billing to shipping test method where account list is available then five account records are created. They are having billing information and this copy billing to shipping is true. So what I am going to do I am just marking it for insert operation. So for insert like we first created the data then we inserted the data and then we queried the updated record and checking the shipping information. If it is matching with test city it means your shipping information is updated. Right. So this is for insert operation. Now I am just copying and pasting it so that it will work for update. So in real-time projects also you can apply this concept like if your one method is doing both the operation insert as well as update. So you can have two separate methods so that you can verify whether insert and update both are working fine separately or not. So I am just cutting it from here. So in this case what is happening we created a list then five account records are created having billing information but that checkbox is false. Then here inserting the data after insert I am iterating a loop on this ACC list and inside this loop I am just setting that checkbox as true and then update ACC list. So on update of this account list trigger will work and billing information will be copied to shipping. Then through this query I am querying shipping information of the first account record that is available in ACC list and that will be available in this updated ACC list. So through that I am just comparing it if it works this assert passes it means your code is working fine. So I am just clicking on run test. So here you can see this is the last one which executed and all the cases are passing. So copy billing to shipping insert test copy billing to shipping update test both are passing. So it means in both the ways code is working fine. So this is the way like you can implement trigger and test class for the requirement that I demonstrated in this video.