 Hello everyone, I am 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, then I will be demonstrating how you can implement it. So upon opportunity record creation, if amount is between 100K to 500K, then we need to populate hot opportunity in the description field. So first, we need to create a trigger on opportunity, then it's handler class and then test class for code coverage. One more thing, here upon opportunity creation, we need to check amount and same opportunity description field we need to update. So here while I will be creating the trigger, so I will be using the before insert event because I don't need to apply any DML because whenever you update the same record through which your trigger is initiated, so you can perform that operation and before insert event. So let's see how we can do that. So first of all, I'm going to create new trigger. So right now in my org, I don't have any opportunity trigger. So I'm naming it as opportunity trigger and from here, I am searching for opportunity. So make sure if in your org, a trigger is already there for particular objects. So don't create new trigger, just reuse the existing trigger. So here I'm using before insert because I need to perform the operation on the same record through which trigger is initiated. Now I'm going to create new, sorry, I need to create new Apex class that will be named as opportunity trigger handler. And here I need to implement the method. So here we will be receiving list of opportunity. So I'm naming it as OPP list. Now I'm going to save this code. So if I want to access it, so here I can call it and I can pass trigger dot new. So whenever you create any record, whether it is one or bulk records, so all the records upon creation will be available in this trigger dot new because here we are using event as before insert. So this trigger will work for insert operation on you. So through this trigger dot new, all the newly created records will be passed to this OPP list. Now here I can apply a loop. So I can iterate on this. Now inside this loop, I need to apply a condition. So OPP dot amount not equals to null and OPP dot amount greater than equals to 100 K and OPP dot amount less than equals to 500 K. So if all these conditions are satisfied, then what we need to do, we need to update the description with part opportunity, right? So this is it. So we don't need to apply any DML operation because this trigger will be executing in before insert context, like your opportunity record is created and it is not saved into database before saving to database, this trigger will be executed and this method will be executed, right? So now for a testing purpose, we need to like check it from the UI. So I'm just moving here, clicking on opportunities. Now I'm going to create new opportunity and I will be testing whether it is working fine or not. And after that, I will be creating a test class so that we can apply bulk test. So OPP 101, then I need to provide amount. So amount I am entering 200 K, then close date and then stays. So we need to populate opportunity name, close date and stays because these are required without populating this. You can't create opportunity and this amount. So if amount is populated and it is between that range, so description will be populated automatically. So right now it is blank. If I hit save, so after save, if I go to details, you will see the description is having some value and it is hot opportunity, right? So it means our code is working fine for one record. Now we need to implement its test class so that we can have the code coverage because when you will be implementing your trigger and trigger handler in the sandbox environment, so for deployment purpose, test class is required and it should be having at least 75% code coverage. So just click on file new, create new epics class and I'm naming it as opportunity trigger handler test. So always remember if this trigger, trigger handler and trigger handler test classes are already there for some scenarios and you need to implement one more scenario. So just reuse these files, don't create new files for each and every requirement. So this is part of best practice. So now, first of all, we need to add this annotation at the latest test. Then for each method, also we need to add this annotation. Now I'm going to write a method. So we need to check with the help of this method, we need to check like whether our trigger and trigger handler business logic is working fine or not. So first of all, we need to create data. So I'm going to create list of opportunity. Now I'm going to create a loop so that I can create bulk records. So always remember whenever you implement your test class, so implement records and bulk so that you can test your business logic whether it will be working for bulk insertion or not. So here, okay, I just missed this packet. So here opportunity, OPP equals to new opportunity. Now, with this instance, I need to populate all the required field information. So first of all, I am setting the name. So test OPP then plus I, so that names will be unique. Then OPP dot stage equals to prospecting, then OPP dot close date equals to OPP dot OPP dot OPP system dot today, right? Why it is showing this error? So something is, okay, now it is corrected. So sometimes when you correct the error, so it shows for some time and when you save or after a few seconds or minutes, it will be corrected if you have corrected the error. So here name, stage name and close data populated. Now I need to populate amount. So I'm populating amount as 200 K. And now I'm going to add opportunities into the list. So data is created, now we need to insert the data. So I'm writing test dot start test and then insert OPP list, then test dot stop test. So if you have more than one methods and if you want fresh governor limits for each test method, so you can apply this DML operation inside this test dot start test and test dot stop test. Now after this, I'm going to query the updated information. So list of opportunity, updated OPP list equals to, then select ID comma description from opportunity where ID equals to OPP list of zero dot ID. So I'm just wearing the first report that is available in this OPP list so that I can make sure whether it is having updated description or not, right? So here I'm going to implement assert statement. So system dot assert equals. So here we need to put two things, expected value and actual value. So our expected value is this hot opportunity should be there. And here you can write updated OPP list. Okay, I need to close it as well. Updated opportunity list of zero dot description. And I'm saving this code. So this way test class is implemented. Now you can expand this and click on tests. Now after implementation of test class, when we use at the rate test test, so this run test button is available at top right corner. Just click on this button. You will see the test execution in this tab. So it is executed successfully because green tick is available. If I scroll, so opportunity trigger and handler, both are having 100% coverage. If you go here, so from here also you can check. If you open this file, so from here you can check. So this way if you implement your trigger, so you must need to implement trigger handler, then trigger handler test class as well so that you can make sure your code is properly covered. So this way I hope you understood this trigger scenario. So I tried to create trigger, trigger handler class and trigger handler test class.