 Hello everyone, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to demonstrate you how we can apply custom validation in Trigger and that custom validation will be applied upon creation of opportunity if amount is null. So in that case, an error message will be thrown on the UI. So I will be implementing a Trigger on Opportunity. Then we'll be implementing a method in Trigger Handler. And then I will demonstrate you how we can implement its test class so that code can be covered. So let's jump to Developer Console. Now from here, I'm going to open Opportunity Trigger. So I have already created Opportunity Trigger because lots of demonstrations I have already done. Here you can see Opportunity Trigger is available which is having lots of events like before insert, after insert, before update, after update. So we will be requiring before insert only. And I'm going to open Opportunity Trigger Handler as well where I will be implementing the method. So here I'm going to write the method public static void. So here I'm writing apply validation and parameter will be list of opportunity and new list. Or we can say OPP list, right? So I'm just saving this file so that I can call this method in the Trigger. So in the Trigger here we need to call this method in before insert. So we just need to pass Trigger.new because on creation we are just calling this. So the name of the method is apply validation. So I'm copying this and I'm pasting it here and saving the trigger. So I just modified this line. So this line is additionally written over here. So Trigger is saving. Now it is saved and I'm moving to this method which is available in Opportunity Trigger Handler. So here I'm going to implement a loop. So for Opportunity, OPP, then OPP list. And inside this loop I'm going to check the condition. So if OPP dot amount is equals to null, then I can write an error message. So if you want to throw an error message you need to call the method add error. And here inside single quotes you can write the error message that you want to display. So I'm writing amount field cannot be null. So this is my error message that will be displayed on the UI. So I'm just saving this code. Now if I move to UI, go to Opportunities and if I try to create new opportunity. So before creating new opportunity I am refreshing the page. So now I'm going to create new opportunity. And while creating opportunity you will see opportunity name, close date and stage is required. So OPP test, then I'm filling close date, then I'm filling the stage. Now amount is blank. So if I click on save so you will see an error message. So here error message is available. Amount field cannot be null. So we didn't provide any value to amount. That is why this error is coming. Now if I provide some value to amount and hit save so the record will be created successfully. So this way if you want to apply some custom validation in your trigger so that is possible. Now if you want to replace this message with a custom label so that is also possible. So what you can do in setup you can search for custom labels. So custom labels are benefit because if you want to modify the message later on. So you can simply modify the custom label and wherever it is used that message will be modified automatically. So it is for opportunity amount and here I'm writing the message and save. So I just need to copy this. And here I can write label dot opportunity amount. So this way I can use the custom label in the code. Again if I go here and refresh the page and try to create new opportunity so the same error message will be displayed. Now the benefit is if you want to modify the custom label so you just need to modify it and the message will be modified automatically. And if suppose you are using this custom label in test class so you need to modify the message single time only. So here you can see the message displayed again. Right now I'm going to implement its test class so I'm going to open opportunity trigger handler test. And here I am going to implement its test class. So we need to create some opportunities and then I will be applying the test method. So here you can see at the latest test annotation is available then public static sorry private static void then apply validation test. Now here opportunity data is created and here I'm leaving this amount as blank. Right and then here insert OBB list is available. So I'm going to remove this code from here then test dot start test. Now if there is an error so that error message will be displayed like if you are testing your code through this test class so that error will be throwing an exception. So what we need to do we need to enclose it in try case so before doing that let me show you. So if I save this code and try to run this test class so you will see an error message. I'm just running this test class. So here you can see it is failing and only one method is failing that is apply validation test. So if I double click and open it so you can see it is showing a field custom validation error exception amount field cannot be null. So now what we need to do to handle this situation we need to apply try catch. So in test class we can apply try catch. So insert is written inside try and after that I'm writing catch. Now inside catch I need to write some code so I'm going to take help from account river handler test where I already demoed in one of the video like how we can handle this custom validation exception. So I'm going to use that code and we'll be demoing you with the help of that code. So I'm just copying it and I'm pasting it here and I'm copying this variable which I need to write before try. So here you can see the variable exception throne is available which is initially false whenever exception is thrown. So this variables value will be true. Now here I have created one more variable that is except expected exception throne. So what it is doing it is just checking whether that exception is containing the error message. So it will be label dot and we can just copy that custom label and we can put it here. So if that error message contains the error that is available in this label so true will be assigned here in this variable. And if it is true so this assert will pass no error message will be thrown while running this test class. So in case our amount is null so exception will be thrown and it will be cast here and because assert equal will be having true and true so it will pass it won't throw any error. If there is amount filled so this catch won't be working it means automatically your insert will be successful. So this way if you want to handle custom validation exception in test class so you can just apply try catch in the same way. And you can see the advantage of custom label so I have used it here in test class as well so it is used in trigger handler as well as in test class. So whenever you want to modify the error message so you just need to modify the custom label only you don't need to modify it through the code. So this is the benefit of using custom label. Now if I want to run this test class so I can just click on run test. And here you can see it is passing so all the methods passed nothing is failing so apply validation test is passing. And if I go here and see the code coverage so code is also covered 100% for this method. So this way I hope you understood how we can apply a custom validation interval that to why creation of particular record. So I demoed it on opportunity so you can just test it for other objects along with different fields. So this is all in this video. Thank you.