 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. So let's understand the scenario first. So requirement is upon account creation or updation if industry is media, then populate rating as hot. So for example, if you are creating new account record, you're populating industry as media. So rating should be hot populated automatically through trigger. Second is you created account. Industry was something else other than media. You save the record. Then later on, you updated the record and set industry as media. So when you update industry as media, so in that case also rating should be populated as hot. So we need to implement account trigger its trigger handler class and then test class for code coverage. So jumping to developer console. So this scenario, the insert part I already demoed previously in one of the video. So what I'm going to do that insert method I will be reusing so that it will work for both insert as well as update. So here we will be using before insert and before update because whenever account rate like industry is updated, we need to update the rating. So on same record, we need to do the change. So I am copying the statement which is available here, update rating method and I'm pasting it here. Now whenever we update. So in case of update, we have two states of the record. One is new and one is old. So new will be available in the trigger dot new and old will be available in trigger dot old map. And from here, we need to pass trigger dot new and now, right? So accordingly, we need to modify the code here as well. So what I'm going to do. So this way it is modified, right? So trigger dot new will be available in this acclist and old map will be available in this old map. So in case of update, we will be passing trigger dot old map. So that is okay. But in case of insert old map will not be passed. So null will be passed and that null will be stored in this old map. So if old map is null, it means insert operation is performed otherwise update. So what we can do, I'm just modifying this. So here I'm writing old map double equals to null and acclist dot industry not equals to null. And so this is a insert part. If you insert the record, so these conditions should satisfy old map should be null. It means insert industry is not null and having value as media. Why I'm checking it because sometimes it may happen like you don't fill anything in industry field. So we are checking if it is not null, then only it will check whether it is having media or not. Then after this, I can have our sign. So if old map is null, then this whole part will be checked. If it is not null, then it will come to the other part. So here also I'm checking acclist dot industry is not null and it should be equals to media. So I'm just copy pasting and acclist dot industry not equals to. So I'm just shifting it here. Not equals to old map dot get acclist dot id dot industry. So what is happening? This is update part. So here if acclist dot industry is not null. So whenever we use ACC, it means we are using new record because acclist is having values from acclist, which is having the new state of the record. So if industry is not null and its value is media, then we are checking acclist dot industry new value with old value like if new value new value is industry. If old value is other than industry, then this condition will be true. And in that case, rating will be populated as hot. Right. So this part, sorry, this part is for insult and this part is for update. So together we are able to manage both the situations in a single method. So now I'm going to save it saving this trigger as well. Now, if I go to the UI and create new account record and populate industry as media rating is blank right now. So I'm just clicking on save. So here you can see reading is populated as hot automatically. Now, if I open any existing record. So here industry is blank. If I populate industry as media. So reading, you can see it is none. So I'm saving it and it is hot by default. Right. And if I open the record, okay, it is already having if I open any other record where both industry and rating is blank. So if I pick any other industry other than media, so you can see nothing happen. So this way our code is working fine in both the scenarios insert and update. Now it's time to implement it stress class. So best practices like if you have insert and update both operation. So you can have two different methods one for insert and one for update. So here for insert, I already have a method. So I'm just searching for that. Yeah. So here it is. So update rating test. So I'm just changing it to update rating insert test. So here list of account is created then five account records are created with the help of loop. So name is test plus I industry here. I'm setting as media. Then account record is added into a CC list. Then we are inserting the record. Then through this SOQL, we are querying the updated account records and along with rating field. If rating is hot, then it will pass. Otherwise it will fail. So this is for insert operation. Now I'm just copying this code and pasting it here. So what will happen? We need to manage this code for update test. So initially we will leave it as blank media is not populated as industry industry is blank. So records will be inserted after insertion. What you can do you can just iterate on this ACC list and here you can paste this code, right? And after that you can write update ACC list. Okay. So this loop will iterate and it will update all the account record values to media and then update will encounter. So it will run the trigger and trigger will be updating rating as hot. So rest SOQL will remain same. Assert equal will remain same. Okay. The only thing we modified like we used insert and update both. So now I'm just saving this code. So I'm going to run the test. So here you will see the results. So tests are passing. So total eight methods are available and all are passing. You can open and you can check them here. So here you can see update rating insert test is passing update rating update test is passing. Okay. So now if I jump here and click on all tests. So here you can see this code is also covered. Right. So this way I hope you understood like how we can implement the scenario with the help of trigger. So I just modified the insert method and that insert method is now capable to perform operations whenever the count report is inserted as well as it is updated. So when common requirement is there for both insert and update so you can write a single method and you can use it for both. But in test class you can have two different methods so that you can test insert and update operations separately.