 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 and test class. Let's understand the trigger scenario first. When accounts phone field is updated, then we need to populate below message in the description field. So this scenario will be based on the update operation. I will be demoing you how you can use the update events in the trigger. So you need to put the below message like phone is updated and old value is this and new value is this. So this format will be available in the description field whenever on account phone field is updated. So we need to implement account trigger and trigger handler. And we need to also write its test class for code coverage. Let's jump to developer console. So we need to implement account trigger for this. So I already have account trigger implemented in my org because for other demonstration, I implemented it. In this demonstration, we need to work on update operation. So here I'm writing before update event. Why before update? Because we need to do updation on the account record only. We are updating account phone and then we need to update accounts description. So before update is sufficient for this. Now here I'm implementing another block like trigger dot is update. If trigger dot is before and inside this block, I will be calling the method. And this method I'm going to define in account trigger handler. So this is the method name. I'm saving this and I'm going to use it here. So account trigger handler dot then method name. Now we need to pass trigger dot new. So here you need to understand one thing like whenever you update the record. So there are two states of that record that record will be having new values. Like there may be chances you will be updating few of the fields. So for those few fields, there will be new value that will be available in the trigger dot new. And the old state of that field will be available in the old map. So we need to pass trigger dot old map. So you can consider like the same record will be available in trigger dot new as well as in trigger dot old map. In trigger dot new that record will be having the newly entered values. And in trigger dot old map, it will be having the old entered or existing values that was already there on the record. So this is important for update scenarios that you need to remember. And here also I need to put map of id comma account old account map. Okay. So now this trigger will save successfully. So now inside this function, I need to implement the logic. So here I'm going to iterate on ACC list. Okay. So in this ACC list, all those records, which I am going to update or whatever records will be updated. So those records will be available with the new values. Now we need to check. So when you will be trading on that account list, so what do you need to do? You need to apply a check whether phone field is updated or not because it may happen. Instead of phone, any other field is updated. So if any other field is updated and phone field is not updated. So in that case, there is no requirement to run this method. Right. Now how we can check whether phone is updated or not. So we need to compare phone fields, new value and its old value. So new value is available in this ACC list because here we are, we are receiving this trigger dot new and old value will be available in this old map. So here we are receiving trigger dot old map. Right. So I'm going to use them here. So if I write ACC dot phone, so ACC will be having the values from this ACC list. So ACC list is having the new state of the value of the records. So ACC dot phone will be giving the newly entered value not equals to then old ACC map. So through map, if you want to get particular record because you can see here key is ID and value is whole account record. So if you want to get the particular account record, so you need to use get method. But here you just need to pass the ID. So I'm passing ACC dot ID. So this account ID. So I already told you the same record will be available in this ACC list and the same record will be available in this old map. Right. So we can pass the ID of this ACC ID. So account record will be received. Now I just need to use the phone field. So I'm putting dot phone. So this way, this is showing ACC dot phone will give you the new phone value and this old map dot get ACC dot ID dot phone will give you the old value of that phone field. If both are not matching. In that case, what you can do, you can apply the logic because if both are not matching, it means phone is updated. If both are matching, it means phone is not updated. Any other field is updated. So that you need to remember. So always whenever you implement this update trigger implementation. So you need to check whether updation is done in particular field or not. Now, here we can write ACC dot description equals to phone is updated old value. Plus now how to vote, how to get old value. So what you can do, you can just copy this and paste it. Okay. Then again, plus in single quotes, you can write new value. Then again, you can write new value. Then again, plus ACC dot phone. Right. So whatever is available in single code that will be displaying as is and these will be replacing with the values and whatever we have in single quotes. It is basically treated as constant. And this, this is treated as variable. So to separate them, we need to use plus sign. So this, this format will be available in the description field. If you update phone field on particular record, if you don't update phone field, nothing will happen or nothing will be displayed in the description. So I'm just saving this code. Moving to UI doing a refresh. Now, if I go to details, so here you can see phone is blank and description is also blank. Now I'm going to fill some dummy value here and clicking on save. So here you can see phone is updated old value null because earlier phone was null. So it is showing null and new value is this. Now, if I update same record again, so I'm removing this and this time I am entering this value and clicking on save. So you can see old value is one, two, three, four, five, six and new value is three, zero, three, zero, three, zero. Okay. Now, if I open any other record, go to details. Right. And if I update rating. Okay. So description, you can see it is having ACC 101. So if I click on save, nothing changed. But if I change the phone value. So the description will be updated. So this way it verifies like whatever code we implemented, it is working fine. So I hope this way you understood how I implemented the requirement with the help of before update even and trigger, trigger handler. Now we need to implement it's test class so that we can make sure the code is covering properly. So I'm going to open account trigger handler test that is already implemented for this account trigger handler class. So here I'm going to implement the test method. Okay. So I just need to test this method. So whenever we implement any method in the test class, we need to annotate them with at the latest test and inside this, I can create the data. So first of all, I need to create account data. So what I'm going to do. I'm going to create list of account because I will be creating bulk data. So always remember whenever you write your test class, so data will be created in bulk. Right. So this way account list is there. Now I'm going to use the data which is already the code which is already implemented so that I can save time because in real time projects. If you are working on a project where existing test classes are available. So you can reuse the code. So we are applying the same approach. So here I need to put the phone value. Okay. So five account records will be created and all the account records phone will be this right now I need to insert this list. So what I'm writing test dot start test then insert a CC list then test dot stop test. So basically you can see in this test class we have lots of methods and in every method somewhere DML operations are available. Right. And all the DML operations are written inside test dot start test and test dot stop test. So why so because if you have more than one methods. And you if you want separate governor limits to be there for each test method. So in that case you can just have your DML inside these test dot start test and test dot stop test. Right. Now account record is inserted but a trigger will run whenever account record is updated. Right. So what I need to do. I'm going to implement the loop again. Okay. So instead of implementing this loop I'm implementing for each loop which will be iterating on this ACC list. And here I'm going to update phone value. So I am adding this value. So whole account list will be having different value and then what I can do I can write update ACC list. Right. And I can just put it here. So here I'm updating account records here inserted account records. Right. Now when this update DML will run so trigger will be executed. Now what we need to do we need to query the account records and we need to check whether those account phone is updated actually or not. Because after update DML that record should be having phone number as this. So I'm creating list of account then select ID phone from account where ID equals to ACC list of zero dot ID. So I'm just querying one of the record that is available in this account list. And if that account records phone is equals to this value, it means trigger work successfully. So here I'm applying system dot assert equals. So we need to provide expected value. So expected value is this actual value is available in this ACC list of zero dot phone. And here I can write phone is not updated. So it is showing some error. Let's check what is the issue duplicate variable. Okay. So here we have already created account list. We are creating it again. So I'm just updating its name updated account list, changing it here as well now saving the code. So this way you can see how I implemented the test class. I created account records, right? Then I updated account record with new phone value and then I varied the updated account records and then I applied the assert equal. Assert is important so that you can validate whether your business logic worked successfully or not. Now here I'm going to test it. So I'm just clicking on run test. So you can see whole test class is passed. And the total six methods are there and every method is passing. So right now we implemented test update account description on phone update. So it is passing. And if I go here and check the code. So you can see the complete code is covered. Right. The coverage is 64% because some of the piece of code on method is not covering because here in trigger. I just marked this as a comment for other demonstration. So the code which we implemented in this demonstration is working fine. Now you might be thinking like if we implement only one method in the test class and we just want to run that. So how we can do that. So you just need to click on test then you run then click on your test class and then select particular method that you want to run and click on run. So what will happen that particular method will be executed. So for example, in your test class, you have lots of method. So if you run all the methods, it will take lots of time to run the test class. So you can just run your one method as well. So here you can see only one method executed and it is passing. Okay. So this is all about this demonstration. So I hope you understood how we can implement trigger scenario with test class for before update operation.