 Hello everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to demonstrate a trigger scenario with the help of test class. So the requirement is we need to prevent deletion of account record if we are trying to delete any account record where active field is having value as years. So we need to implement its trigger as well as trigger handler class and then we need to implement its test class so that we can have a proper code coverage. So jumping to developer console. So for previous demonstrations, I already created one account trigger. So I'm going to open it. So if you go to trigger, so you can find account trigger if you have followed all previous videos. So this is account trigger which is already implemented. So we need to prevent deletion. So here we need to apply one more event that is before delete. So before deletion, we just need to check whether that active field is having us or something other. So if it is having us, so we need to prevent the deletion. So I just added this event. Now I'm going to open account trigger handler class as well. So account trigger handler, this is apex class. And here I need to implement the method. So these all are existing methods which I implemented to demonstrate you some scenarios. So if these methods are not implemented, so that that is also okay, you can just implement a single method in your class and you can call that into a trigger. So method name is prevent deletion, right? I will be calling this method in the trigger. So here I'm going to create another block for deletion. So if trigger dot is delete, then if trigger dot is before. So in case of delete, we have both the events before delete as well as after delete. So right now I'm just implementing before delete. So I just implemented this block. Now I'm copying the name of class account trigger and the method name is prevent deletion, right? So whenever you delete any record, so new record won't be created, right? Because whenever we delete any record, so we are actually deleting the existing record. So it means from here you just need to pass trigger dot old. So trigger dot old contains the record which is deleted. It will be automatically available in this trigger dot old context variable. So we need to pass it. Now here we can receive this as a parameter. So list of account and I can write ACC list. So I'm saving this method or this class. Now I can save this trigger as well. So if you are implementing this trigger to just test this deletion operation, so these instructions won't be available in your trigger. So you can just write this piece of code and you can write before delete as well in the first line. Okay. Now I'm going to implement the code here. So all the data is available in this ACC list. So it can be possible you are deleting only one record or maybe you are deleting more than one record. So we always implement trigger for bulk implementation. So if you are deleting bulk record, then also your code should work properly. So here I'm writing a loop for account ACC then colon ACC list. So we are going to iterate on ACC list. So one by one each account record that is deleted will be available into this ACC variable. So here I'm going to apply if condition. So I just need to check ACC dot active underscore underscore C double equals to yes. So this active is a field which is available on account. So if you want to check, you can just go to object manager. Then you can just open account object and here you can find this field active and it is a pick list field. And if we open this field, so here you can see it is having two values. Yes or no. Right. So I just checked here if this active field is equals to yes, then here I can just write ACC dot add error and I just wrote this message active account cannot be deleted. So if account record is active, then we won't be able to delete that. So I'm just saving it. So this way trigger is implemented. So this piece of code we implemented in the trigger and this method we implemented in the trigger handler class. Right. Both are saved now moving to UI. I'm going to create. Sorry, I'm going to delete an account where active is yes. So I'm going to details and here you can see active is no. So let's delete this account record first. Okay. So this account is basically associated with the case and if any case is already available with any account, so we won't be able to delete. So let me open another one. It is not having any case associated and active is nothing. So I am just populating no here saving this record. So it is ACC 101. So now I'm going to delete this record. So you can see it is deleted and when you delete any record, so it goes to recycle bins. So we can just open it in your tab so that we can see the deleted record. Here it is. Now I'm going to delete another record where active is yes. So here I am changing active from no to yes. Right. And it is ACC 1102. So if I try to delete this record, so here you can see active account cannot be deleted. So this way our trigger is working fine. Whenever we try to delete any active account record, so this error message will be available on the UI. Right. So now after implementation of this trigger, we just need to implement its test class as well so that we can just cover this code. So I'm just going to open account trigger handler test. So test class is also implemented. So I just implemented it to cover existing methods that I implemented to demo other scenarios. So here I'm going to implement one more method which will be covering the method which we implemented here. So let me just copy the existing implementation so that we can reuse. So I'm just copying it. So at the rate as test and then I'm pasting it. So here we need to create simple data. So we went delete test. So five account records are created and active as yes here. Okay. Now we just need to insert the list. Right. And after insertion, we need to delete. So here I'm writing test dot start test test dot stop test. Right. So inside these two statements, if you write any dml statement. So in our test class, you can see we have lots of methods. So for each method, if you want to have a fresh set of governor limits, so you can just write dml inside test dot start test and test dot stop test. Right. So the records are inserted. Now we just need to delete these records so that error will be thrown. So if I write delete a CC list, so I'm saving this code. Now after deletion, we just need to apply a cert operation as well. Right. So let's see what happens if we run this piece of code. So I just saved it and I'm opening test. So this tab is already opened and I'm clicking on run test. So all the methods will be running and here you can see it is showing error and this prevent deletion test is failing. And here you can see it is showing the error delete field. Why so? Because of field custom validation exception and active account cannot be deleted because we are trying to delete active account record. So that is why it is showing this error message. Okay. So our code is working fine. Like if I go here and try to see the code coverage, so it is covered. Right. But our test class is failing. So what we need to do, we need to apply try catch here so that this issue can be handled. So if delete is throwing any exception, so we need to handle it. So it may possible like you are trying to delete any account record which is having active field as no. So in that case, this, this test class method won't be failing. But if you're trying to delete any account record which is having active as yes. So this method will fail. So you just need to enclose this delete statement into try. So that if any error is coming one time error is coming so that can be handled here. Right. So delete will be executed. So because of that, this method will be covered. And if there is any exception like runtime error, similar to like custom validation exception, so that can be handled here. So what we need to do, we need to apply catch exception. And now inside this catch block, we need to write the statement. So what I'm going to do, I'm creating a Boolean variable before try. And it is initially false. Now inside catch, I'm changing it to true. Right. So if like this statement delete is throwing any exception, so it will be converted into true. Then Boolean, one more variable I'm going to create. So expected exception thrown equals to then E dot get masses dot contains. Now here in single quotes, I need to pass the error message. So I'm going to copy it. I'm going to paste it here. Right. So if error, like error is available in this E, E dot get masses dot contains this error message. So what we are going to do, so here we have additional single quotes. So I just removed. So basically here I'm going to use a conditional operator. So if this is true, so it will be returning true. Otherwise false will be returned. Right. So this is basically a condition. If error message is containing this message, so it will be returning through otherwise false. And accordingly, we can apply assert. So here now we need to implement system dot assert. So system dot assert equals then here I'm writing true. Then this variable. And then masses will be E dot get masses. Right. So if true is matching with this expected exception thrown variables. So if it is also true, it means this assert will bypass, right? It won't be showing any masses. And if it is false, then it will be showing this message which is available in this get message. Right. And after this, I'm writing one more assert equals. So I'm just copying it, pasting it. So true then exception thrown. So I'm just copying this variable. And here I'm going to write no exception was thrown. Right. So if any exception is not thrown, like we are not entering into catch. So this variable will be false. Right. So here we are matching true and false. So it is not matching. So it will be shown on the like results. So it will be showing no exception was thrown. If exception is thrown, so it will be true. In that case, it won't run in that case. Like we will be checking whether that error masses will be like having this error message. If it is having then it will be true. So in that case, it will be bypassed. If that error is not available, so it will be false. So true false. So this will be executed and the other error masses that is available will be displayed. So here it is showing some error masses. So I need to check it. Okay. Contains spalling mistake. Okay. I just need to remove the sign. Now it is implemented correctly. And if I run this now. So this time you can see all tests are passing. There is no issue. And if I open it. So there is no error message. Okay. Because we just applied try catch. So that is why it is working fine. So basically error masses, this active account cannot be deleted. It is available in this e.getmessage. So here we are receiving true. So true true. It is matching. So whenever both values are matching, so this assert equal don't throw any output. If both are not matching, then it throws some result. So this way I hope you understood like how we can apply try catch as well in the test class. Right. And in the trigger handler class like we just applied add error. So basically known as custom validation as well. Okay. So I implemented this scenario to demonstrate you how we can implement before delete operation. Or you can say how we can implement custom validation along with how we can implement try catch in the test class. So three different things are covered in same scenario. So I hope you understood this scenario well. Keep watching more scenarios so that you can master yourself in trigger and test classes. Thank you.