 Hello, everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So this is the first video of trigger and test class scenarios playlist. So as per the demand of viewers, I'm creating this playlist so that if you have learned about the basics of trigger and test classes, so how you can implement scenarios based on them. Let's understand the first requirement. So here you can see a trigger scenario is written. Which is upon account record creation. If industry field is having value as media or energy, then we need to populate rating as hot. So industry and rating both our fields. Those are available on account. These are standard fields. So whenever we create a new account record and industry is having media or energy. So in the rating field, we need to populate value as hot. So we need to implement three things for this requirement. So first we need to create a trigger that will be on account. Then we need to implement trigger handler class where business logic will be implemented. Then if you want to implement the code coverage for the trigger and trigger handler class. So you need to implement test class as well. So I am going to demonstrate everything in this video. So let's jump to developer console. So those who already completed basics of trigger and test classes, they must know like how we can open the developer console still if you're a beginner. So just click on this gear icon and from here you can open the developer console. So here we can implement all the code. It is inbuilt ID provided by Salesforce. So to accomplish the requirement, first I need to create a trigger. So to create a trigger, you need to click on file new and then select Apex trigger. So I'm creating it on account. So that is why I'm naming it as account trigger. Then here I'm selecting object as account and clicking on submit. So this way trigger file is created. Now here in trigger we have seven events. So right now the requirement says we need to check this condition upon account record creation. It means we can have one of the event from before before insert and after insert. So there are two events which are available for insert. So we can either choose before or we can choose after, but for this requirement, I'm going to choose before insert because if you again read this requirement. So on account record creation basis on the industry field, we need to populate rating as hot. So we are just focusing on the record through which your trigger will be triggered. So in that case, we don't need to do any modification or position in the related record. So if you are doing modification in the same record through which your trigger is triggered. So in that case, you can use before insert event. Now here we can implement the business logic, but the best practice says always implement business logic in the handler class. So I'm going to create one class naming it as account trigger handler. So best practice also says like you will be having only one trigger on one object and whatever business logic you have. So you can implement it in a trigger handler in separate functions and those functions you can call in the trigger, right? So here I'm going to write public static void update rating and here I will be receiving list of accounts in the acc list variable. So now I'm going to save this account trigger handler class and I'm going to call it here. Right now one more thing whenever you create any record and your trigger is initiated. So those newly created record, whether you create one record or you create more than one records or maybe bulk record. So all those newly created records will be available in this context variable, which is known as trigger dot new. So it is basically a list. It will be containing all the new records. So those new records with the help of this trigger dot new. We are passing into this acc list where we are going to implement all the business logic. So I'm going to save this. Now here I'm going to apply a loop. So loop is a trading on this account list. So this account list will be having some records. So one by one each record will be available into this acc variable. Now what we need to do, we need to check whether this acc industry is equals to media or industry is equals to energy. So we have two values media or energy. So basis on that we need to update the ratings. So we can write acc dot rating equals to pot. So this way condition is applied. So this is basically our business logic. So here you can see I have not implemented any DML. So if you want to update the same record upon its creation with the help of trigger, so you can use before insert event and in this business logic function, you don't need to write any DML because this trigger will be executing before the record is saving to the database, right? So you don't need any DML rating will be updated automatically. So you can see account trigger is saved. Account trigger handler is saved. Now before implementing its test class, I just want you to see like how we can test it from the UI. So if I go to UI and click on account step, I'm going to create new account and here I'm typing industry as energy and at top you can see rating is done. So I'm going to save it. So I'm going to details and here you can see rating is odd. So it means this code is working fine for single record. Now without implementation of test class, if you want to test it through code, right? So we have one more way like in this anonymous window, you can create bulk account records and then you can insert them and from the UI, you can check whether rating is updated to hot or not. So for example, so I created account list. Then I'm iterating a loop. So I'm creating five account records. So here account ACC equals to new account and ACC dot name equals to test plus I then ACC dot industry equals to energy and then what you can do, you can add these individual account reports into list and then you can write insert ACC list. So this way you can insert bulk reports and you can check whether your implemented code is working fine or not. So I'm going to execute this code and now if I come here click on accounts tab. So from list view, I'm selecting new this week. So these five records are created here. Now I'm going to add one field here. Not one actually to one will be industry and another will be rating and save. So here you can see for these records energy, sorry, industry is energy and rating is hot and this is ACC 101 that we created from the UI. So this way you can test this code after creation of bulk reports, right? So through single record as well as to bulk record creation, you can test now if you want to implement it's test class. So first of all, why test class implementation is important because if you implement this trigger in your sandbox environment and if you want to deploy it to production environment, so test class is required and at least 75% code coverage should be there. So this I hope you have already understood through my test apex test scenarios. If you have not understood that, so you can go through with that first, then only you will be able to understand these scenarios. So I'm going to create one apex class. So its name will be account trigger handler test. So I'm clicking on okay, right? So you need to focus on the naming convention as well. So my trigger handler class name is account trigger handler. So if you want to create test class for this, so you can name it like this, right? So that when you search for account trigger, so account trigger, its handler and its test class both all things will be searched automatically. So if you click on file and click on open resource, so here basically we search. So if you search like this account trigger, so you can see trigger handler, trigger handler test, everything is available here automatically. So now when you implement test class, so you need to use annotation at the rate test test before the class. And if you create any method, so for that as well, you need to create, you need to write this at the rate is test annotation. Then so we need to test the rating. So here I'm going to write this method update rating test. Now inside this test class, first we need to create the data, then we need to apply DML and then we need to query the report like whether they are properly updated or not because of that trigger and then we need to apply assert. So first of all, I'm creating the data. Right. So what I can do, I'm just copying this piece of code. So here you can see this list is created. Then through loop, I am creating five account reports adding them into list. Now after this, I'm going to write insert. ACC list now in this class, you can write these two statements as well as thought start test and test thought. Sorry, stop test. So why we use these statements? So for example, in your test class, if you have multiple methods, so right now I have only one method, but if I have one more method. So this way, if I have more than one methods, so if you want governor limits should be re-initiated for each method. So in that case, whenever you perform any DML operation, so you can enclose them in inside test dot start test and test dot stop test. So for this method, fresh set of governor limit will be available for another method. If you are using test dot start test, test dot stop test, so governor limits will be refreshed. If you don't use test dot start test and test dot stop test, so whole test class, whatever number of methods are there. So they will be like using one set of governor limit. So I'm just removing this code because it is not required right now. So here I created sample data then insert data, right? So always remember whenever you create any test class, so first of all you need to create the data then apply DML operation so that trigger will be initiated. Now after that, you need to query the updated data. So I'm creating list of account, updated account list equals to then here I'm writing select ID rating from account where ID equals to ACC list zero dot ID. So what I'm doing, I'm actually querying the first record that is available in this ACC list. If that particular record is updated, it means other records are updated successfully, right? And if you want to query all the records, so that is also possible. So now here apply a cert condition. So you need to write system dot assert equals now here. So if I write it again, so here you can see we need to write expected value and then actual value. Okay. So expected value is hot and actual will be available in this updated ACC list of zero dot rating. Right. So this way I implemented this assert equals. Okay. It is saying variable does not exist. So, okay. Here I need to add D as well. So it is updated account list. Now both are matching. Okay. So this test class is saved now, right? So here at top right corner, you can see when I added adhered is test and save this test class. So this run test button is available. If I go to this class here, you won't be seeing that run test button. So now you can open this and click on test here. The test result will be available. So just click on run test. So test class is executing. So if you see this green tick, so you can be happy like your test class passed successfully. We we implemented one method. So here you can see total one. It means it is passing and at bottom right corner, you can see account trigger is 100% covered account trigger handler is 100% covered. Right. And if you want to see the code coverage from here, so just open the trigger, click on this drop down and here you can see all test 100%. So whatever code that is covered through test class, it will be available in view. If it is not covered, it will be available in red color. Highlighted in red color. If I go to this account trigger handler, so here if I click on all tests, so you can see all lines are covered. So this way always remember whenever you create your test class, so create bulk data and then apply assert equals as well because if assert equal is not passing, it means your test class is failing. Right. So this is all about like how we can implement trigger, trigger handler and trigger handler test class so that we can have the poor coverage. So I hope with this demonstration, you have understood this scenario.