 Hello, friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So this is my website studiesaleforce.com. So here you can find links of various videos related to Salesforce on a single platform. So in this video, we are going to discuss how we can apply validation to add error method in case of apex trigger. So I will be covering various scenarios here. So I will tell you like how we can apply validation condition in these scenarios. So I will tell you first like in before insert how you can apply validation then before update then before delete and then after insert. Right. So after watching this video, you will be able to understand how in various scenarios we can add error message to a particular record. So first we are focusing on before insert. So here you can see all events are already available before insert update delete and before insert before update before delete and after insert. So as per your requirement in your project so you can increase or decrease them. Then I'm checking if trigger context is is before and then trigger context is is insert. So this method will be executed. Right. So I'm updating this one because I'm not going to update description. So I'm going to just update rating. So for this I have implemented account trigger handler and in this account trigger handler you can see first this method is available public static void update rating and it will be having a value in new list. So new list will be having values from trigger dot new context variable. So whenever you are inserting new record so those will be available into new trigger dot new and those we will be having a new list. Right. Then we are iterating new list and if industry is not null and industry values media then we are putting rating as hot. So now I'm going to tell you how you can apply validation here. So we are checking if ACC dot industry equals to null. So ACC dot add error. And here we can write please populate industry. Right. So this will be error message please populate industry or please fill industry value. Accordingly you can put so if industry is null then this message will be displayed. Otherwise this else will work. It will check if industry is not null and it is equals to media so hot will be stored else. So in case of media only in case of media rating will be hot. Otherwise, rating will be cold. So this way you can control this thing. So whenever you try to create new account record and you put industry as blank on null. So this error will be displayed otherwise accordingly hot and cold will be stored. So I saved this account tracker handler and saving account trigger. So right now only this method will be executed because we are going to create new record. And I'm commenting this one because otherwise this after insert will also work. So now I'm going to create new account record. So let's save. Let's first get it saved. So now you can see this trigger is saved. So I'm going to create new account record. So clicking on new and typing account name as test and I'm not putting any industry value here. So it is none. So clicking on save. So here you can see please populate industry. This error is automatically added on the record and you won't be able to save it until you provide a particular industry. So if I put industry as communications. So now I'm saving this record so you can see a record is created. And if I go to detail, so here you can see rating is as cool. And if I select industry as media at the time of creation of the record, so rating will be having hot as a value. So this way I explained you and demonstrated you how on before insert you can add that right. So I hope this way you understood how it is working. Now let's move on update. So this was before insert. Now we are on before, before update. So in case of before update. So if you want, like if you are if you are modifying any value and let's say here phone is available. So if you try to modify phone on the account record and phone is null after updation. So in that case, we will be adding the error methods. So in case of before insert, sorry, in case of before update, we require two parameters. New list and old map. So if we move to the trigger. So here you can see this method. So here trigger dot new is passed, which is having new records and old map will be having the old state of the records. And here the condition is if old map is null. Sorry, old map is not null and ACC dot phone. This is the new value and old map dot get ACC dot ID dot phone. So through old map we are getting the value of phone and we are comparing new and old values if they are not matching. It means you updated the phone. Also, we can add one more condition here if ACC dot phone that is the new value is null. Right. So let's say on account record earlier there was some value as phone. Now you updated your record and this time your phone value is null. So in that case, you want to throw an error like if you're modifying the phone. So you need to put some phone number. You can't make it as null. So in that case, we are going to put one error message. So ACC dot add error. You cannot make phone as blank. Right. Or you can add more message like please provide a phone value or whatever way you want. You can modify it. So I'm closing it here then else. And inside else. We can put this. So this way I implemented or modified this code. I'm saving this. So if you are updating your phone and it is null, then only this error message will be displayed. Otherwise description will be having this message phone is modified on account and the updated phone will be available on the description. Right. So moving on to the record. So here you can see initially phone is blank. And description is also blank. So I'm going to modify the phone number. So one, two, three, four, five. So this time from null to we are assigning some value. So no error will be thrown. And if we see the description, you can see phone is modified on account and one, two, three, four, five is the current value. Now, if I make it blank and click on save. So here you can see you cannot make phone as blank. Right. So here is added here as a validation. So this way on before update, you can also apply validation. And if you put any particular value, let's say 111 and click on save, then it will work properly. And again, you will see our description is modified automatically. So this way I hope you understood how we can apply validation condition in case of before update as well. So next is before delete. So if you are trying to delete any account record. So all the deleted account information will be available into older list. So we are calling this check account status method in this trigger here and we are passing trigger dot old context. So here we are checking if on account active is true or yes, then this error message will be added otherwise account can be deleted. So here you can see account is blank. So right now I'm adding one value clicking on save. Now I'm trying to delete this account clicking on delete so you can see sorry you cannot delete accounts which are in active state. So this message is available. Now I am changing its value to know clicking on save. Now this time if I try to delete this record so you will see this account will be deleted automatically. So account test was deleted. So this way you can add error message while deleting any record. So I hope you understood how we can add error message in terms of validation through add error method on before insert before update and before delete. Now it's time to after insert. So I'm uncommenting this. So in case of after insert we are passing trigger dot new because new list will be available here. So here you can see we are fetching new list in this variable then opportunity list is created then we are iterating on new list. Then we are trying to create new opportunity whenever an account record is created. So what is happening. First time creating instance of opportunity then filling name account ID stays and closed it and account ID will be ACC dot ID. So whatever account record you will be created. So immediately an opportunity will be linked with that account and after completion of this loop. Let's say you created an account. So for each account one opportunity will be created and those will be added to this list. And after completion of this loop those all opportunities will be inserted. Now the requirement is we need to add the error message right. We want to add some error message. Why creating opportunities right. So what I can do I can write if ACC dot active is not equals to yes. So in that case I want to add error message. You cannot create account as an active right so this will be the message. So if active is not equals to yes. In that case this message will be displayed you cannot create account as an active. So please populate yes on active. So this kind of message you can put if account is already having yes as value so else you can write these instructions. So this way you can see this method is modified now. So whenever account is not active then this message will be displayed otherwise one opportunity will be created and it will be linked with that account. So this way you can control like whenever account is active then only opportunity will be added otherwise not right. So I am saving this. Trigger is already saved so now if I try to create new record clicking on new writing here as test and here you can see. Yeah active is having nothing and industry is also having nothing right. So first before trigger error will be here please populate industry. So we need to populate the industry otherwise we won't be able to see that error. Now if I again click on save so you can see you cannot create account as an active. So this error is coming from the after insert and if I populate industry. Sorry if I populate active as yes then it will be saved. And here you can see in the related list when opportunities created automatically. Right. So this way I hope you understood how we can add error message in case of after insert. So here one thing you need to remember like everywhere you can see in case of before triggers as well in case of after trigger. We are adding this error to this new list. Trigger dot new right so whenever you want to add error so you need this new list that we are getting from this trigger dot new or maybe trigger dot hold right so in case of delete you need trigger dot hold in case of insert and update you need trigger dot. So on that only you can add the errors so that they will be reflected on the record so here you can see on old list we are trying to add error here in new list we are trying to add that and in case of before insert also we are trying to add error in new list. So on new list in case of insert update and hold list in case of deletion, you can add the error so that they will be displayed on the UI. So this way I hope you understood how we can apply the validation rule in case of trigger with the help of this add error method. If you want to watch more trigger apex or process automation related videos so you can go to this website study salesful.com. So here you can find all the video links those are created by me related to Salesforce. So watch them and learn Salesforce. Thank you for watching this video.