 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta tech school. This is my website study salesforce.com. So here you can find all the Salesforce related videos links together on single platform. So you can visit that in this video. I'm going to discuss and demonstrate about how you can use before update and after update in a big trigger. So in my previous video, I demonstrated you about before insert and before, sorry, before insert and after insert. So here in this video, these are two scenarios. Those are for before update and after update. So first take a look on before update. So here the scenario is if account phone is updated then put update masses in description. So whenever account record on account record, if you are updating the phone number. So we need to put a masses in the description field that your phone number is updated. Right. So this we want to perform in before update. So as you saw in my previous video when we used in before insert. So this was the method update rating and I defined that method in apex trigger handler. So here you saw, whenever we perform before insert. So we don't require any dml operation so same as with before update. So when we will be implementing before update method so we don't want to write any dml separately. So, because record is not saved so before saving the record. If you are applying before insert or before update, then your field can be updated automatically. So now I'm going to update the trigger. So here I'm writing one more block that is if trigger dot is update. Right. And inside this block I will be checking whether it is before or is after and here I need to use these events. So before update. So after update. Right. And I'm just copying this block and pasting it here. So trigger dot is before trigger dot is after. So here I'm going to call a method as update phone description. And here I'm going to write update. Related opportunity phone. So that is our other another scenario that is for after update. So if account phone is updated then populate that on all related opportunities. So that I'm going to demo later on. So first we will be discussing about this update phone description. So right now here you can see two error messages are available because these methods are not defined. So now I'm moving to trigger handler. So here I'm going to define the methods. So public static void. So name of the method is update phone description. So I'm pasting this then list of account list. And one more thing that we are going to use here that is additional parameter trigger dot old map. So in the introductory video related to trigger, I told you like we have these two context variable trigger dot new that is having new list that you have updated. And the existing records information will be available in the old map along with the ID. Right. So we need to check new state or phone field as well as its existing state. Right. And then only we will be updating the description because there may be chances like you are updating the account record and you didn't change phone. So if phone is not changed phone is not updated. So we don't want to execute this method. Right. So that's why we are going to compare new list phone value and old map phone value. If both are unequal than only this trigger definition will be executed. So here I'm going to use map of ID account and naming it as old map. Right. So this is the method where I need to implement the logic. So now I'm going to implement a loop. So loop will be for account ACC new list. Right. Now inside this new list I'm going to apply if condition. So if old map is not equals to none. So this will be first condition. If old map is not null, it means you are performing update operation. Right. And then we need to compare new phone value with old. So what you can write ACC dot phone. Not equals to so ACC is the variable that we are going to use in this loop, which is having new list information one by one. So let's say in new list you have five records. So all record one by one will be available into this ACC. So if ACC dot phone that is a new state of the phone is not equals to now we need to check the old state. So what we can do old map dot get then ACC dot ID. So whenever you are using map to retrieve the value, we need get method and get requires a key. So this is the key that we are passing ACC dot ID and get will be returning the value and here you can see value is account. You can see here value is complete account record but we need only phone field. So I am writing phone here. So this is the complete if if old map is not equals to null. It means you are updating the record and if ACC dot phone. This is the new state of the phone that you updated and this will be having the existing value. So if both are unequal, it means on the account record you updated the phone field. So here I'm writing ACC dot description equals to phone is modified on account right and I need to close it in single quotes. So this way I implemented this before update trigger and the method is update phone description. So whenever I will be updating the phone field on the account. So this method will be executed with help of trigger. So now I'm saving this So here you will see automatically this error will be gone at line number 11 right so I'm saving this as well. So line number 13 will still show error because this method is not defined yet. So I'm just commenting this one because right now we are going to test the before update thing. So again I'm saving this so now all the errors will be gone. So this is the time to check account records. So I'm opening one account record. Let's say test trigger five moving to details. And here you can see phone is not available blank description is also blank. Now I'm updating phone value. So 123456 and I'm clicking on save. So here you can see phone is modified on account. So this way you saw whenever I updated phone field. So description is updated automatically right now let me open any another record. Let's say test trigger for so this is another one here phone is blank and description you can say you can see it is also blank and I'm just updating type. Let's say installation partner. So as I updated type so trigger didn't work update trigger didn't work because we are not updating the phone field so you can see description is still blank. So this way this condition is very important whenever you are implementing before update or maybe after update so you need to check whether specific fields are modified or not then only you can apply the logic. Otherwise what will happen if if we remove this thing if we remove this thing. So what will happen whatever field you are changing or updating this trigger will be updating description always right. So to restrict that updation whenever any other field is also updated so we need to put this condition. So I hope this way you understood the working of before update and here you can see before insert is also available. So here only new list is available and we are updating the rating field and in before update we are updating the description and in both the scenarios we are not performing any other insert or update. So this is the only case that that is available and before insert and before update because in that case the trigger dot new that we pass the trigger dot new that we pass in case of before insert or before update is editable. So we are passing trigger dot new into this new list and through this new list we are fetching the values in ACC and we are updating the ACC. So basically internally these are connected with referencing. So if you are updating ACC it means trigger dot new is automatically updating right so in case of before trigger this trigger dot new is editable so that's why we don't require any additional dml. So I hope you understood the working of before update. Now this is the time for after update. So in case of after trigger. So after trigger fires when your record is saved in the database. But remember one thing it is not committed yet because your record is saved then after or triggers will work and then some more operations will be executed and then your record is actually committed in the database. Right so whenever any failure occurs so a whole process is rolled back automatically. Right so remember one thing whenever your record is saved after that after trigger works and before that before trigger works. So now in case of after update we will be requiring dml operation so that we can update the opportunities. So now whenever phone is updated we want to populate that on opportunities as well. So moving to our logic. So let me go here. So I'm just checking my process builders. Okay so here you can see on account we have two processes so both are inactive. So yeah no problem. So now we can work. So why I checked because in my earlier process builder demo I created one process builder that automatically updates account phone or any other field to opportunity. So that's why I confirmed that. So now I'm going to uncomment this and here also I'm going to pass trigger dot old map. Right so this method I need to define now in trigger handler. So I'm moving to trigger handler. And here I'm going to write public static void pasting the method name and whatever I passed in this update so I'm going to use this here as well. And here I'm going to write the definition. So now what we need to do whenever account phone is updated we need to update the related opportunities as well. So I'm going to iterate a loop. And before that I'm going to create a map map of ID and let's say account. So account ID to account map equals to new map of ID comma account right now while iterating this loop while iterating this loop what I'm going to do. So first let me write this thing. So new list right now I'm going to check that condition. So inside this loop I'm going to check this condition. And if old map is not null and account phone that is new list value and old map is fetching the existing one if they are unequal it means phone is updated. So in that case I'm going to put the values in this map. So in this map what I'm going to do I'm basically putting a CC dot ID. It means account ID and account record completely. So I'm just filtering those records where phone field is modified. So it may happen like from data loader you are updating various records and only in few records phone field is modified. So we are just filtering those records only right. So this way this map will be having ID account ID and account record for those records their phone field is modified. So that information is available into this map. Now we need to patch the related opportunities that we want to update. Right. So here I'm going to create one list. So it will be list of opportunity. So opportunity list equals to new list of opportunity. So now what I'm going to do. I'm going to use this opportunity list list equals to then select ID phone from opportunity where so on opportunity we have one field that is account ID. So account ID in now I'm going to use this map dot he said so whenever we use this method key set so it automatically generates a set of keys those are available in the map. So that set will be available here and through in we can check a particular account ID in that set. So if on a particular opportunity the account ID is available in this set. So those are opportunities will be available into into this OPP list. Right. So it is showing one error. Why so because I mentioned this as incorrect. So now this error will be gone. Right now we can iterate one more loop. So for opportunity OPP and this OPP list we can mention here. Right. So what we can do we can do one more thing. We can do one more thing like we can put this complete query here as well in place of OPP list. So what I'm going to do I'm I'm using this here. So all the opportunity records will be queried where account ID in this key set. So one by one OPP will be available here and now we need to remove this thing as well. And I'm renaming it as opportunities to be updated. So those opportunities which we are going to update the information will be available into this list. Now I'm going to check if condition. So you can see opportunity will be available here. So what I'm going to do. Now through this opportunity I'm fetching I'm going to fetch account ID. So and then account ID I'm going to check in this map. And if that account ID is available in this map then I'm going to fetch the phone value from this ACC or the value that is available in the map. So first of all I'm going to use this contains key. So through contains key we can check whether a particular key is available in the map or not and key will be OPP dot account ID. So the opportunity that we queried if it's account ID is available in that map. It means this opportunity is somewhere related to the account that is available in that map. So now we can update this. So here I need to create one, sorry one more opportunity instance. So opportunity OPP or equals to new opportunity. So this I created now if that key is already there. So what I'm going to do opportunity dot ID equals to OPP dot ID and OPP or dot OK. So let me check the phone field that we are going to use on opportunity. Okay, so we are going to use this phone that name is account phone on opportunity. So here I'm going to use that field so it is a custom field. And here, yeah here I need to populate the phone number from the account. So now I'm going to use this map dot get. Now I need to pass the key so key will be this and get method will receive the value. So value you can see the complete account record is so dot and you can write phone. Right. So through that complete account record you just want to fetch the phone. So phone will be stored in this and yeah this is created now. So if this condition will be true. Then only we want to add this opportunity into the list. So list we created here. So if it is true, then you can write dot add and OPP or. Right. So after completion of this loop, whenever this if condition will be true, so updated opportunities will be available into this list. Now, after completion of this loop, what you can do. There are also opportunities spelling is incorrect. Right. So after completion of this loop, you need to check if not then opportunity to be updated this list is empty. If it is not empty, then you can apply update. And then list of me. So all those opportunity records will be updated. We are the related account phone is updated. So this way now I'm going to save this. Okay, so it is showing two errors. Okay, let me search for this account ID is current why it is showing. Select ID phone from opportunity where account ID and let me see the error again. Okay, okay, got it. So this phone field is showing the error because I need to use this phone. Now I'm saving this and saving this. So now you can see both are error free because no star is available and no error message available in this problem tab. So this way, now we are ready to execute this. Right. So when I will be updating phone on account. So you will see both will execute this will also execute because this will be updating description field on the phone and this second method will be updating the related opportunities. This phone field. So moving to account. So let's check this one. So on related we need to check whether important opportunities available or not. So yeah, it is having one related opportunity. Right. So let me create one more. So I'm creating one more opportunity on this test trigger five. So test. Five dash two. So this is another one. I'm putting up close date stage name I already provided so now you can see two opportunities are available. Right. Now I'm moving to details. So on this, this phone is already available 123456. So let's move to related and I'm opening these two opportunities as well. So that we can check what is the existing value here. So if I move to details. So here you can see account phone is blank. And for this one account phone is also blank. Now I'm going to update account phone here. So now it is 123456 I'm writing 654321 and clicking on save. So it is showing. Okay, so now here you can see if something is incorrect in your trigger. So this kind of error will be available so it is showing account trigger. Account trigger execution of after updates off for after updated is showing error caused by system dot s object exception s object row was retrieved by SOQL without querying the requested fee and the field is opportunity dot account ID. So it is further showing class dot account trigger handler update related opportunity phone line 11 column one and for account trigger line 13 and column one. So if we move to account trigger. So here you can see online number 13 we are calling this method. And if we move to account trigger handler so online number 51. So online number 51. So here it is saying we are using this account ID, but we are not querying it. So if you're using a particular field for any process so you need to query it in SOQL. So here it is showing the error. So as it is having error, it means before operation was was not completed so it will be rolled back as error is available in after update. So now if I click on save so you will see no error will come record will be saved successfully. The phone is modified on account messages available and I am refreshing this opportunity refreshing this opportunity as well. So we will see 654321 this phone value will be available on both the opportunity records that are related to account. Here you can see so we are on opportunity that is test trigger five dash two and here you can see account phone is available if we move here. So here also you can see in details this account phone is available. Now if you again modify this, let's say we are adding this value click on save. So automatically the related phone will be updated related opportunities phone will be updated. And if you modify any other field except phone so this updation won't be working because we already filtered the records where phone is updated, then only related opportunities should be query. So here if I see on the details so you will see. Yeah, so it is updated automatically. And here also you can see the details. Yeah, 111 is added four times at that. Now if I modify any other thing, let's say I'm modifying type. I am modifying type clicking on save. So if I refresh the opportunity both the opportunities so you will see the phone field won't be affected. It will be having the same value. Right, so this way I hope you understood how we can use before update and after update. So I showed you all the things together as well. So this block is for insert before insert after insert and this block is for before update and after update and accordingly you can call the methods. This may also happen like for before insert, you have multiple methods so here you can call any number of methods. Right, so this way you can create the blocks and then call any number of methods and in the trigger handler you can write the proper definitions, whatever operation you want to perform. So this we implemented in this video for updating account description if phone is updated. And here you can see this is another method that we implemented which is updating related opportunities phone if a parent account phone is updated. And this way we are filtering those only those accounts for which we want to query the related opportunities. So I hope you understood whatever I tried to demonstrate in this video. So I hope now after watching this video you are familiar with before update and after update thing. So also you notice that in before update we don't write any dml like insert update but in case of after update we need to write insert update or whatever dml on same record or any any related record we can do that. So this is the basic difference between them and in case of before update. And after update we also pass old map trigger dot old map but in case of before insert and after insert we only use trigger dot new because in case of insertion existing state is not available. But in case of update existing as well as new state of record will be available. So for comparison purpose we can use both trigger dot new and trigger dot old map in case of update records. So in my another videos those will be available after this. I will be demonstrating delete triggers and delete triggers. Then I will be implementing all before triggers like before insert before update before delete in single video and after insert after update and after delete in single video. So this way I will try to compare each and every scenario so that you can understand trigger value. So thank you for watching this video.