 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is my website, studysalesforce.com. So here you can find all video links related to Salesforce. So you can watch them. And in this video, I'm going to demonstrate you the implementation of before insert, before update and before delete together on a particular object. So I will be implementing it through a fixed river. So let's first understand the scenario. So for before insert, I will be updating the account description. So whenever a new account is created, so we will be updating the description of the account, right, then when account report will be updated. So when we are inserting any accounts, so this description will be there as soon as you update the account and its phone field is updated. So then you need to update the description again, having a text like phone is updated. Right. So let's say, in case of insertion, we can say account record is created and in case of updation, we can say our phone is updated. And third is before delete. So here you can see a requirement is mentioned like show error when an active account is deleted. So on account I have one pick list that is active. It is having two values no and yes. So if it is having yes then we need to show the error like you cannot delete the account otherwise account can be deleted. So now moving to the org. So here you can see I have created account trigger on account and three events I'm using before insert before update and before delete. So here I am going to check one if condition if trigger dot is before and inside this I'm going to check. trigger dot is insert. These are trigger context variable that I'm going to use else if trigger dot is update and else if trigger dot is delete. Right. So these are three blocks now and here I can call the respective method that I want to execute. So I already created account trigger handler. So one by one I will be explaining the methods. So this is the first method that is for update rating or so in my scenario I told you like we will be updating the description. So here we will add that as well. So update rating and description. Right. So I'm little bit modifying the scenario. So this method we need to call so in insert we can use the name of class account. And dot name of the method then trigger dot new. So trigger dot new contains all the new records those are inserted. So this list we are passing and that list will be received by this new list. Then we are iterating this new list with the help of this loop and we are checking if account dot industry is not null and industry is equals to media so we are setting rating as hot and we can also do one thing. ACC dot description equals to account is created. So this message will be available in the description. So let me cut it from here because we want this description to be populated every time whenever a record is created. Whether industry is null or not null or it is media or not. So this if condition will set the rating as hot. Otherwise, rating will be none and description will be populated as account is created. So I'm saving this So I'm going to create new record. So I will be creating two records. So let's say test ACC one here I'm sorry I'm not putting rating. I'm just putting industry. So if industry is not null and it is media then rating will be hot automatically. So here you can see rating is blank right now and description is also blank. So I'm clicking on save one error is available at line number four method. Okay. So I updated this method so I just I'm just pasting it here and trigger dot new I'm saving this as well. Now I am again clicking on save. So here you can see record is created if we move to the details rating is hot automatically and if we go down. So here you can see description is having account is created right now. Let me create one more account. So this time industry will be blank. So still you will see still you will see description will be populated. So I'm not putting any industry. So you can see industry is none. Clicking on save. So again you can see one more account is created if we go to details so rating is blank because industry was blank. So that if condition won't work. So at the bottom you can see description is still populated as account is created. So this way I hope you understood how we can use this before insert. So in case of before insert you don't want to apply any dml because this trigger dot new that we are using or we are having a new list is editable. So whatever you want to perform some modification here you can do so that will automatically done by the trigger. Right. So this is before insert now moving to before update. So I'm copying this method here. Account trigger handler dot method name. So whenever you are going to update any record. So there are new records or new state of record will be available as well as old state will be available. So trigger dot old map. So new list is passing the new state or new values of the record and old map will be passing the old values. So this we will be having in this method. So new list is having new state and old map is having old values then new list we are iterating if old map is not null. It means we are updating because old map is having something it means record is updated. Then we are checking the phone. So ACC dot phone. It means new phone value and it is comparing with existing value. So old map dot get. So we are getting the value through get method. Then we are passing key as ACC dot ID. So value will be this complete account information. So from that we just want to fetch phone. So if new phone that is available through ACC dot phone and old phone that is available through this if both are different. It means phone is modified. So we are updating the description and we can also put older new values as well. So let's say is phone number is modified and equals to and I am using the ACC dot phone. So this will be the new value that I am going to concatenate. So as you know we created two accounts and both description are having account is created. Now I will be updating the phone and you will see description will be updated with this phone is modified on account and this phone number will be available. So I am saving this. So only phone field whenever phone field is modified then only this description will be modified. Otherwise not. So I am moving to the account. So this account we created right now. Test account too. So I am going to update the phone. So right now it is blank. I am putting 1, 2, 3, 4, 5. Clicking on save. Moving to the description so here you can see phone is modified on account and phone value is also available. Then again if I modify this. So let's say 54321 and if I click on save. So again you will see the modified value is available here and if I move to test account one. Right. And if I open the details and let's say I am modifying the facts. Right. I am modifying facts value and at the bottom you can see description is account is created. I am clicking on save. So this time description won't be updated because we are not modifying the phone value. So this way you understood why this condition is important because if phone is updated then only you want to update the description so that you need to check this condition here. So that's why old map is required and new list is having the new states. So this way again you saw we modified this new list only and this new list is having values from trigger dot new. So again this is before update or you can say before trigger. So here trigger dot new is in form of editable mode. So you can update that as well directly. Now third scenario is whenever we try to delete any account and if account active field is having value. Yes. So we need to show an error message. So let me first all this method here. So account trigger handler dot then this method. So in case of deletion we don't have any new state of the account. So we have only old state. So I'm passing trigger dot old. So this you need to remember. So trigger dot old will be received through this oldest that is list of account. I'm iterating it then checking each deleted accounts active field value if it is yes then this error will be added. So add error is a method that will update a particular record with this error message. So this will be this error will be displayed whenever you are going to delete the account where active field is having value as yes. So here if we see active is blank. So first time putting active as yes clicking on save. Now I'm going to delete this account. So you will see an error message. So are you sure you want to delete this account. So I'm clicking on delete. So here you can see sorry you cannot delete accounts which are an active state. Right. So this is test account one. Now I'm moving to test account two. And if I move to details. So here you can see it is having active as false. So now if I try to delete this record. So it will be deleted. No error will be thrown because active field is having nothing. So this way I hope you understood again. We didn't apply at any DML. We just modified the oldest and oldest is having trigger dot old. So this is also in form of editable mode. So in case of before trigger if you are using trigger dot new context variable or trigger dot old context variable. So you can modify their values and you don't need to write any insert or update DML statements. So they will be modified automatically. But if you are moving to after trigger then you need to perform DML operations because in that case trigger dot new and trigger dot old are in form of read only. So this I also discussed in the introduction video of related to trigger. So this way I hope you understood all the three scenarios that I demonstrated for before insert before update and before delete right so more videos you can watch on study salesforce dot com. So here I put all the links related to videos those I created for Salesforce so you can watch them as well. So I hope you understood all three scenarios well. Thank you for watching this video.