 Hello everyone, I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to demonstrate a scenario based on trigger. Pardon me. Let's understand the requirement of trigger scenario. So when an account record is created and copy billing to shipping checkbox is checked. So this copy billing to shipping is a custom checkbox field that we need to create on account object. If on account record creation this checkbox is checked. Then we need to copy account billing address to shipping address. Right. So we need to create trigger on account then trigger handle class and then I will be showing you like how we can implement the test code coverage with the help of test class. So jumping to developer edition also developer console is already opened. Now in my org I already have account trigger created. So in one of the other scenario I created this. So here you can see account trigger is created event is before insert. So for this requirement also we need before insert because on account record creation we just need to copy account billing information to shipping information. So this trigger will be useful then this trigger handler here I will be implementing one method. And after that when I will be implementing test class I will be opening that before that I need to create a checkbox field as well. So I'm opening the setup. So an object manager under account object we need to create a field. So I clicked on new and I'm selecting checkbox clicking on next. So field label is copy billing to shipping and clicking on next. So making it available for all the profiles clicking on next and it will be available on the layout as well and then I'm clicking on save. So this way what happened the field is created now I need to go to developer console doing a refresh so that newly created field will reflect here. So again I'm going to open account trigger and its handler. Okay. So now here I need to implement one method. So in this demonstration you will also get to know like in one trigger how we can implement more than one business logic so public static void. So I'm naming it as copy billing to shipping. And here we need to use list of account ACC list. Right. So here also we use named as name as ACC list so you can reuse it because it is a different method. Right. Now I'm just saving this for being this method name and here I'm going to reuse it. So first of all I'm copying and pasting it. Again I'm copying this method name and pasting it here. So this way now this trigger will be executing two methods. One is updating and one is copy billing to shipping. So now it's time to implement the code here. So what I need to do I need to implement a loop and inside this loop I need to check the condition. So if ACC dot now we need to have that field API copy billing to shipping. So I'm just going to search it. Okay. So in this org copy billing to mailing is there. We created copy billing to shipping. So I'm copying it pasted it here. So if this checkbox is true then only we need to copy billing information into shipping information. Right. Now if I take you to the account record. So on account record in the details section. If I added so here you can see billing address and shipping address. And they have street city state postal code country. These five fees are there. But if you search here like address so you will find billing address and shipping address. So if you use these API names directly so it will be incorrect. Right. So let's say I copy this. So if if I use it like this. So this is incorrect. If you write the statement. So if you try to save it you will receive an error message so you can see it here. Field is not writable account dot shipping address. Okay. So this is incorrect. So we need to remove the statement instead we need to write ACC dot shipping city equals to ACC dot billing city like this individually you need to copy the fields ACC dot shipping country equals to ACC dot billing country ACC dot shipping, postal code equals to ACC dot billing, postal code then ACC dot shipping state equals to ACC dot billing state, then Shipping street equals to ACC dot billing street. So this way you can copy the information, right? Now you can save it. So our business logic is implemented. Now this trigger is already saved. Trigger handle is saved. Now moving to UI. So I'm going to create new account record. Okay. And I'm enabling this checkbox copy billing to shipping. And here I'm writing test street, test city, test state, sample postal code and test country. And I'm clicking on save. So record is created. If I go to details. So you can see checkbox is checked and whatever is available in billing address, it is available in shipping address as well, right? Now, if I try to create one more record and enable this checkbox and this time suppose billing address is blank. And if I click on save, so this time you can see nothing happened, right? So if you want to apply this condition like if this checkbox is checked and billing information is not blank, then only information should be copied. So what you can do here you can apply and condition and you can write ACC dot billing city not equals to null. So like this, you can add conditions. So right now what will happen if this checkbox is checked and billing city is not null, then only this piece of code will be working. So this way you can apply other things like billing country, postal code, billing state, billing street as well in this condition check, right? So this is best practice. Unnecessary or code should not work. So you need to take care of that as well. Now I'm going to show you how we can implement the code coverage for this. So this is account trigger handler test class, which is already implemented. And here we have this update rating test method. Now I'm going to implement one more method. So let me implement it in the beginning so that it will be visible on the whole screen. So you can implement after this method as well or before order doesn't matter. So public static void copy billing to shipping test. So now here I need to implement the code. So first of all, I need to create the data. So what I'm going to do, I'm going to copy this code and I'm pasting it here. So if you already have test methods implemented, so you can reuse ports in later examples, I will be demonstrating how you can use at the rate test setup and test utility classes to minus the data as well. So right now what we are doing ACC list is created and through this iteration five account records are created. So I'm just removing this ACC industry because it is not required here. Instead, I'm writing ACC dot. Sorry, billing city equals to test city ACC dot billing country equals to test country ACC dot billing postal code equals to one, two, three, four ACC dot billing state equals to test state ACC dot billing street equals to test street. So these information I provided now what I need to do. I need to populate the checkbox copy billing to shipping as true. Right. And this postal code. I think I need to put in single quotes because it is showing some error. Now this copy billing to shipping is true. Right. So if it is true, then all this billing information will be copied to shipping information. Right. Now this list is inserted after this. We need to apply assert. So what we need to do, we need to query some data. So I'm applying list of account updated ACC list equals to then select ID comma. So I'm just going to query shipping city from account where ID equals to ACC list of zero dot ID. So here you can see we created this account list, which is having five records. So the first records ID I'm comparing here because if this record is updated, it means all other records are also updated because we are using this DML operation. So DML performs operation on either zero records or all records. Right. So if one record is updated, then we can consider like other records are also updated. So after this, we can apply assert. So system dot assert equal. So here we need to put expected value and then actual value. So expected value for shipping city will be test city. So here I'm putting it. And actual I'm writing updated ACC list of zero dot shipping city and I need to enclose this single quote here and then semicolon. So this way this test method is implemented. Okay. So this is our complete method. Now I can execute it. So I just saved this test class. Right. So both the test methods will be executed. So I'm just expanding this. Clicking on tests and clicking on run test. So here you can see our test is passing. Total two is available here. Right. And if you expand so here you can see both are passing. So if you have more than one test methods in your test class, so both will be executed if anyone is failing. So your whole test class will fail. Now if I go here, so you can see it is showing like with which method with which test method this trigger is covered. So overall coverage is 100% and both the triggers are covering it 100%. Right. And if I go here, so you can see this update rating test is covering 58% of code and a copy billing to shipping test is covering 91 and overall 100% code is covered. So if I click on this one. So because of that, you can see this piece of code is covered which is highlighted in blue and rest red is not covered because with this update rating test here, we have not populated any billing information. Right. It is not working for that, but if I click here, so through billing information or test method, this method is also covered, but here industry is not populated. That is why it is not covered. Right. And if I click on this option, so 100% code is covered. So it means test class is implemented correctly and whole trigger and trigger handler code is covered. So this way I hope you understood how we can create trigger for this requirement and trigger handler and how we can cover the code with the help of test class.