 Hello, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School So today we are going to discuss about a big trigger and I will be doing some demonstrations around before update and after update events, right? So This is day number 46 and I hope if you have gone through yesterday's recording, so Recording all live sessions, so you have practiced all the questions those I mentioned there, right? So now We'll be discussing about before update and after update. Okay, so Here If if you have joined this session for the first time, so You you can just go through with this slide and you you can you can understand what I have done and What I'm doing for the community so with this slide you will be able to know and These are seven virtual learning best practices. So basically you need to focus on these so what I Hope you are applying all these best practices while implementing the scenarios, so Like if if you are attending sessions regularly, then only you will be able to understand the things So if if by any chance if you miss any sessions So you can just watch the recording and if you have any doubts So you can just ask ask that doubt in the comment section or telegram group that I have created Right, so I can see lots of people are utilizing telegram group and healthy Conversations are going around and people are helping each other So if you have not joined that telegram group, so that telegram link is available in the description of this video so you can just go through and Like I always say consistency is the key So you you just need to do regular practice if you do regular practice then only you will be able to become the export So today Today's session I will be first explaining how to implement particular scenario and then I will Do some QQ and A's so whatever question you will be asking. So I will try to answer those questions right moving forward So This week we will be completing week number 12. So total three months will be completed for this bootcamp so that is good and Moving forward so here you can see lots of Social media platforms are available those you can follow if you if you want to receive timely notification and Other details about other boot camps and important links are available in videos description So you can always go and check Okay, so with this like please share This channel link with everybody because from 20 April we are going to start cybersecurity bootcamp and Maybe in couple of days I will be sharing those details with you Okay, so today's topic is before update versus after update So yesterday we discussed like how we can implement before insert and after insert So I compared both both the events now today We will be first implementing before update and then I will show you how we can implement after update Okay, so there is one scenario which is available related to before update so In case of before update What you need to do you just need to identify whether The scenario that you are going to implement it involves one object or more than one if it involves one object then you can just keep it in before update because in that case you don't need to perform any DML operation and Before the record save to the database you can do certain modifications So in case of before update you won't be applying DML operation So here is a simple example which says if account phone is updated Then you need to put update message in the description Right, so you can say like phone is updated or you can provide old value as well as new value, right? So now I'm going to implement this scenario in front of you so that you can understand how actually we can Define before update and trigger Right, so jumping to developer console and here you can see we have a counter already implemented So first of all what I need to do in the first line. I need to write before update So I need one event that is before update. So that's why I'm adding it in the first line Now after that what we can do We just need to define if condition where I'm writing trigger dot is Update Then again if condition right, so what will happen? Here you can define whether it is before or after Okay, so trigger dot is update and trigger dot is before now here we need to call the method So this way I just created the block so that we can call a method and before update, right? Now moving to account trigger handler. So here Yesterday I implemented three methods Right, so total three methods are implemented here. Now. I'm going to implement one more method above So here I'm writing public static void and update phone Okay, so This method I'm going to define now this method. I need to call so basically if if we are Working with update operation. So in case of update operation, there will be two different values for particular field on the record Right. So if I try to call update phone, so from here, I need to pass trigger dot new So trigger dot new And I'm passing trigger dot Old map I'm passing trigger dot old map. So if I take you here on the UI and If I try to open our sales application So under the sales application We have this accounts tab and here if I open any account record If I go to details, so here you can see phone is right now blank if I click on edit and Provide some value. So let's say I'm filling one Particular number of time. So this is my new phone number, right? It is test data. So there will be two different values So initially phone was blank. That is one value then after modification you provided some other value So that is new value. So the same record the record which you are updating it will be having two copies The record which we are updating here. It will be having two copies one with the old values and one with the new values So there may be chances like you are updating particular fields, not all the fields So the fields which you are updating only those fields will be having old and new values Rest other field will be having same values in both the copies Right, so I am repeating it again. So whenever you update the record, so there will be two different copies available for the records so Trigger.new will be having the new copy with the new updated values and trigger dot old map will be having the old values So that's why I'm using trigger.new here. So trigger.new is basically a list and Trigger.oldMap is basically a map Right, so here instead of writing old map we can write trigger.old as well But trigger.old is a list and we need map because we will be searching something in the map So in list we won't be able to search particular value But in case of map if we pass ID, so we will be able to search particular record in that map So new will be containing the new copy and old map will be containing the old copy of that same record So the record which you are updating that record is available with trigger.new as well as that record is available with trigger.oldMap and The field which you have updated its new value will be available in trigger.new and its old value will be available in trigger.oldMap Right, so I hope this way you are able to understand it now here what I need to write first I need to create a list of account acclist and then map of id comma account and Here I'm writing accmap Okay, or I can write acclist old map So it will be containing old values and acclist will be containing the new values Now if I save this method So right now I have not implemented any block of code. So I will be implementing that Before that I am just saving the trigger so that we can see whether it will be saved or not Yeah, it is saved correctly now here. I can implement the logic Right, so what I'm going to do I will be iterating a loop on acclist So this acclist is containing all the new records one by one They will be iterating and values will go to this acc variable now what we need to do we need to check so there may be chances you are updating a record and You are updating any other field other than phone This may possible Right, so if you are updating any other field other than phone in that case this method should not run In that case this method should not run or should not do any operation for that particular record And if phone field is updated then only this method should Work for that particular record. So here in this if condition we need to check Whether phone field is updated or not. So the new value of phone we can have through acclist So it is ACC So we can write ACC dot phone and then not equals to so here This ACC dot phone will provide you the new value of that phone field Now we need to identify the old value of that phone field Right, so old value of that phone field is available in the old map ACC old map. So what I can write ACC old map dot get and we need to get ACC dot ID So the I already told you same record will be available in this list and in this map Right, so through this ACC variable We are passing the ID into the map and map will get the account record whole account record associated with that ID Okay, but we need phone field. So here after dot I am just writing phone So here what will happen? This ACC dot phone will give you the new value of that phone field and this ACC old map dot get This get method will give you the old value of that phone field So if both are not matching if both are unequal it means for particular record you have updated phone field Okay, so now what we need to do we already compared like new value and old values are different so we need to update the description and Here I am writing Phone is updated old value equals to then plus and Here I can write this thing then again plus new value equals to and ACC dot phone So this way both old value and new value will be populated in the description field Right, so this is our code that I have implemented here So in case of update operation you need to pass two parameters one for new list and one for old map and I hope you understood why we used old map because through old map searching is fast We don't need to write one more loop here for comparison. So if you want to Resolve nested loop issue. So in that case you can just go through map Right. So now I'm just saving this code and If I go to the UI so here I'm updating phone field So you can see description is saying account is created But if I update phone field, so you will see the result. So I just clicked on save So description field will be updated So here you can see phone is updated old value equals to null and new value equals to this sample value that I provide Right. So this way our code is working fine And I hope you understood the difference between this account list and old map So the record which we are updating its new copy will go to ACC list and its old copy will go to this old map and whenever you need ID and That particular record combination like key and value pair So you use map and if you use only List so you will be getting the values and you cannot apply searching operation on that list Okay, so let me take few Questions So Ravi, I think I already used map in form of old map. So it depends on the requirements So if you are using update operation, so in that case, you will be using old map. So Ritu Raj I Ritu Ja, I think I am already Explaining like when to use list set and map. So in this example, we use list and map So it depends on the requirements. So if you do practice, then only you will be able to understand this thing Because you cannot memorize like here. We can use list here. We can use set. So it depends on the requirements So just understand the concept of let's list set and map that I think it will help you out So you are asking for map. So map is basically key value pair So I hope you missed the session where I explained about map. So just go through that Because I already explained that in detail. So if you go through that, you will be able to understand So Kumar, you are asking survey updated same value means I didn't get your question what you are want to ask So Chandan, yes, ID will remain same in case of before update and after update because we are working on the existing record Okay, so today I can see Very less questions are there. So I'm just moving ahead and going to implement next scenario. So Next you know it next scenario is about before insert and update. So If you remember yesterday, I implemented the scenario like on account creation if industry is not null and having Value as media. So we need to populate rating as hot. So now I'm going to modify the same code for Insert as well as update. So with this example, you will be able to understand like if requirement is same So how we can use one method for insert and update? So with this example, you will understand how to use same method that is available in trigger handler So how you can call that same method into before insert as well as before update, right? So now I'm jumping here So we already have this method implemented. So if you see at the bottom, we have this method So I'm just cutting it and pasting it on top Right. So this is the method that I implemented yesterday You can go through. So it is right now implemented for before insert. It is not for before update But now the same method I will be updating and it will work for before insert as well as before update Okay, so here I need to pass one more parameter. So map of ID, comma, count ACC old map. So this way I just modified it Now I will modify the code as well. But before that I'm going here. So from here we need to pass trigger dot new And we don't have any old map. So second parameter will be null Insert we won't. So I think connection broke. So I'm just repeating again. So if we call it here Populate rating. So we just need to pass two parameters One is trigger dot new that will be containing all the newly created records and second parameter will be null because we won't be having old map Now I'm just copying it and pasting it here And from here we can pass trigger dot old map Right. So here we have two methods And this populate rating we are calling in before insert as well as after insert Sorry, before update. Before insert and before update So this way same method you can call it two places Now what we need to do here we need to decide whether we are having this record created as newly created record or like it is updated If it is newly created then we can check the condition of the record This record created as newly created record or like it is updated If it is newly created then we can check the condition and if it is updated So we need to check whether industry field is updated or not So we will be checking its old value as well as new value Right. So first I'm going to apply for this insert operation So how we can identify like it is I think video got free so I'm just re-initializing it I think now it will work fine So basically what we need to understand here In the same code we need to decide whether record is created or it is updated So whenever record is created so you already know in case of insert we are passing second parameter as null So it means if old map is null then you are doing insert operation So if ACC old map double equals to null And so this is our one condition This is our one condition so I'm just enclosing it in parenthesis So if old map is null industry is not null and industry is equals to media So this is our first condition then or and then inside bracket So if old map is null then this part will be checked If old map is not null it means update operation is happening So in that case what you can do you can write ACC.Industry Not equals to ACC old map dot get and you will be getting account ID And through that you will be getting industry field So what is happening here we are just checking whether industry field is being updated or not So if it is updated then only rating should be hot otherwise not And here we need to check one more thing So I'm just writing and ACC.Industry equals to media So this will be our condition So you can just implement this code in your system and you can just test it and verify So this first piece is for insert and second piece is for update And logic is same So this way I hope you understood how we can create one method for both before insert and after Before insert and before update Let's see some questions I can see few questions are there Yes Rituja existing means old map ID And Kumar is asking same phone number updated means how will work I didn't get your question I didn't updated the same value it was earlier null and then I provided new value It was already in edit mode that's why you didn't recognize But I updated from null to some value So Ananta ready what error you are getting if you use ACC old map Maybe some syntax is not properly written just check it So Faris is asking if I add a new required field as an admin via setup Do I need to update existing trigger or will trigger work fine without code update So if you have created a required field and your trigger is creating new record In that case trigger will break so you need to update your code So Chandan if we don't pass null so we won't be able to call this method Because this method that we are calling it expects two parameters This method needs two parameters so if you don't pass null so that method won't match So at least you need to pass two parameters So Jaki is asking do we really using trigger in the dynamic flow area Yes for complex implementation we always prefer trigger Thank you being Morissa So Ruchit we use old if we need old values list We use old map if we need that same list in form of map So both will be having same data old and old map will be having same data But old is a list and old map is a map that is the only difference And I think everybody knows the difference between list and map So moving ahead now I am going to implement one more scenario So it is for after update and this scenario I will be implementing with the help of map and parent to child So those who are asking like how we can define map in trigger So I am going to implement the solution with the help of map So the requirement says if account phone is updated then populate phone number on all related contacts So if parent account record phone number is updated so all the child contact phone should be same So we can implement this solution with two ways using map and using parent child SOQL So first I will demo you how we can implement it through map And then I will show you how we can implement the same concept using parent child SOQL Okay so I am jumping here and first of all I am going to implement a method So we need to update related contact and here I am going to have both So we will be having list and we will be having map So I am just saving this code so that I can call it So now here I am defining else if trigger.isAfter And here I am writing account trigger handler dot and that method And here we need to pass trigger dot new and trigger dot old map So this way after update I am going to call it and here I need to write after update So always remember whenever you are writing any method you are calling any method after update So you need to write that event in the first line as well Okay so I hope everybody is good till here And now here I need to define the logic Okay so basically on phone update we need to update the related contact So it means phone account phone will be updated from the UI And all the contacts should be updated through the code Through the trigger So here I am going to first of all create a list of contact Because whatever contacts I will be updating those I need to store somewhere So I am just creating a list of contact Right and here I am going to create a map as well So map of id comma account and it will be account map New map of id comma account Right so this way I just created a contact list and I created an account map Right now I am going to iterate on the account list So we have ACC list So here also what you need to know you need to understand Whatever record we are updating so that record is having two copies One is new and one is old So new copy is available in this ACC list and old is available in this ACC old map So right now we are iterating on this ACC list Right so one by one record will go to this ACC variable and then we need to apply the logic Right so here what I need to check I need to check whether account phone is updated or not So I am writing if ACC dot phone not equals to Not equals to null and ACC dot phone not equals to Or maybe I think I can remove it I can just check directly So it should be ACC old map dot get ACC dot id And from here I am getting phone Right so this way we are just comparing whether phone field is updated or not If it is updated if it is not equal it means old and new values are different So what we can do we can just store that particular account whose phone is updated into the map So we created this map so in this map I am just putting all the values So ACC dot id and ACC Right so this way you can write a comment as well like this block will store some values and map Where account phone is updated Right and we are validating it through this if condition and we are just modifying the map Okay so I hope this way you understood it clearly Now what I need to do after this loop I just need to query the contacts Okay so I am going to query the contact and SOQL I will be applying in the first line of the for loop So here I am writing select id and I will be updating home phone field of the contact Then account id from contact where account id in So what we are doing we are trying to get all the contacts which are related to the account And who what account which accounts the accounts which are available in this map Right because we are just checking if this condition is true It means for that particular account a phone is updated and that account information is available in this map So what I can do I can write ACC map ACC map dot key set So all the keys will be available here and through that all the contacts will be queried So all the contacts will be queried where account id is available in this maps keys Right so this way I am just getting all the contacts which are related to particular account record Now here I just need to apply if condition so what I am going to do I will be checking So here I am checking whether the contact will be having account id populated if that account id is available in this map So what we need to do on that contact we just need to populate home phone And from here a phone value will come so it is available in the account map So ACC map dot get then here we can pass con dot account id and then dot phone So this way here I am leveraging the map functionality So through map first of all I used key set so that I can fetch all the contacts Then here I am getting the particular phone field through that map and I am assigning it into the contact phone And this contact I am going to add into con list Right now after completion of this loop I can write if con list is not empty Then I can write update con list Right so this way you can build your solution with the help of map So now if you have any question you can ask just place your question in the chat so that I can answer your question So here I didn't use any parent to child SOQL and nested loop is also not implemented Because everything we are managing with the help of map So let me just go to the question answers So Rituja yes if you want to update existing record at that time you need to use old map only So Ruchit is asking 11th line so in 11th line So just read this comment what is happening I am just storing only those accounts where phone is updated So through this if condition I am checking whether phone field is updated or not If phone field is updated then only I am adding those records into map So this map is different line number 6 Map we are using at line number 11 and this map is different it is ACC old map And this is just a ACC map so this is the basic difference Yes Sachin you can use database class to perform DML operation that you already know Okay so Vamshi is asking why we use map in row number 18 So basically this loop we are iterating on contact We don't have anywhere account phone number So account information is available in this map that we created at line number 11 So through that map we are actually getting the account phone And how we will know like which account we need to fetch So that account ID we are passing that is populated on the contact So Nikhil is asking why we use key set So key set basically gives you all the keys those are available in the map So basis on that we are just querying all the related contacts So Nagahari before update and before insert before insert I already explained yesterday Before update we are discussing right now and if we combine them So this way you need to implement So first you need to check whether your old map is null and other conditions And in all part you can provide this comparison So this way it is implemented So the idea is if you combine both functionality before insert and before update together So in that case you just need to check whether insert operation is performing or update operation is performing So where we through SOQL we cannot update any record SOQL is basically used to query the records not to update So now our requirement is we just need to modify this code So that we can implement parent child SOQL So in that case you won't be using map your code will be working with the help of parent child SOQL So I was asking couldn't understand line number 11 So it is very simple just read this So what is happening we are just checking at line number 10 If old phone or new phone is not equal So we are just adding that particular account record into map So it is very simple So line number 16 is basically checking whether Account ID that is populated on contact that particular account is available in that map or not Contains key means whether particular value is available Whether particular key is available in the map or not So I think like people are not following So have you gone through the map lesson map session that I explained So if you don't follow all the session then there will be difficulty So just follow all the session and don't don't join any random session Otherwise it will be very difficult for you to understand So I already told you all the sessions are connected If you miss any session I can't help So I am consistent I am delivering content on regular basis So it is your responsibility to learn it on regular basis Otherwise I can't help So someone is asking what I am teaching So I don't know the answer I am teaching nothing So she was asking using set instead of map so easy for me because I always So if you confuse that doesn't mean you won't be using map Where you need map you need to use map If you are getting confusion do more practice of map Instead of saying like I am confusing So just practice map there are lots of scenarios available for map So just practice and you will be able to understand Yes Janna we can ignore line number 16 because we are already providing this Keyset here but for safe side I just applied that if condition So I just request please follow all the session If you join random session so I won't be able to help you much So Bindo is asking we used update in this function But previous for phone update you didn't call it why So that was before update So I already told you in before update DML is not required This method we are calling in after update So in after update DML is required So Ritu Raj you are asking about this map So I think you guys didn't you are not following all the sessions That is very said Like you are struggling in basic statement So it is basic statement of map So get is a method which get the values And value for this map is account record whole account record So that account records phone field will be available with this statement It is very simple So in get method we are just passing the account ID So it will return the whole account record because its value is account record And through that account record we just need phone field So this is mentioned here And guys this is humble request if you follow all the session When only attend this bootcamp because it is not for fun It is not any comedy show like anytime you saw like notification And you joined and you think you will be able to understand So I am not doing any comedy show it is very Like it is learning session So if you are interested you are welcome If you are not interested then there is no foundation But mark my word if you have custom coding knowledge Then only you can get a job in Salesforce ecosystem Because there are very less jobs for admin So do regular practice try to implement complex scenarios Try to understand things I am explaining everything line by line So watch the recordings understand implement the scenarios So here also for this session I created a exercise sheet So I am doing lots of efforts But I don't know why people are not following it properly So just follow it do all the practice self practice exercise is also there So when you will be implementing them then only you will be able to understand Otherwise no one can help Okay so Grinath is having one question So let me just share the Q&A screen So Grinath is asking if we have 10 records 10 records in objects 2 records are updated how many records So if you are updating 2 records So those 2 records will be in trigger.new And the same 2 records will be in trigger.old So Ravish Swarnkar is asking my question is that If we use the parent child SOQ query And using for loop on contact and perform the update operation That I will be explaining after this Yes Sachin this guy asked very good question And as per the question I am not able to provide any answer So map or parent child which one is better So map is better because in parent child you will be using an additional loop Okay Ruthuraj if you follow all the sessions or do more practice Nikhil is asking is it necessary to write line number 22 So line number 22 is yes we need to check whether it is blank or not If it is blank you will encounter an error while updating the contact list So just make it part of your best practice So Veera my suggestion will be it is little bit confusing But if you do practice you will be able to understand So coding is difficult but through practice you can make it simple And here you can see lots of practice questions are available So just go and practice so that you will be able to understand them So now I am going to modify this code using parent child SOQL So through parent child SOQL you won't require a map So I will be doing little bit tweaks in this code So I will be removing few lines and will be adding few more So here first of all I am going to add a set of ID and naming it as ACCID So I just created a set then we already have a loop which we are iterating on ACC list Here we have this condition where we are checking old and new values So here instead of map so we don't need map so I am commenting it And this also I am commenting and I am writing ACCIDs.add ACC.ID So instead of map I am going to use set Now here I need to implement parent child SOQL So this code I am commenting and here I am going to implement one more for loop And this will be for account and here I am going to write select ID phone So this is parent to child SOQL So for account we are getting ID phone for contact we are getting home phone This is child relationship name then from account we are ID in ACCIDs So this is parent to child SOQL We are querying accounts whose account ID is available in this ACCIDs And this is a set we created above So wherever phone is updating that phone that account ID will be into this ACCIDs And basis on that we are querying account records along with contacts But now here we need to implement one more loop So I need to apply first if condition if ACC.context not equals to null So if particular account is having some related context So this context list won't be null If it is not null then I can write a loop Context.con colon ACC.context Now here I am writing con.homephone equals to ACC.phone So ACC is actually having the account record information And that account record phone we are going to populate on contact home phone So here we have two loops one for account and one for related contact Now after this I can just add the updated contact into this contact list And rest of the code will remain same So this way you can just update your code through parent to child SOQL Okay so this way I hope you understood And if we compare both so map is always fine because in map You don't need multiple loops nested loop So your processing time will be less in case of map Okay so let me see if there are more questions So Greenath I already answered your question If you have 10 records and you update two records So two records will be in trigger.new and two records will be in trigger.old The same two records which you updated So in trigger.new it will be having with new values In trigger.old it will be having with old values Okay line number 27 So line number 27 is for contact loop So whenever you are querying account with related context So this ACC.context will give you all the context related to particular account So one by one those contacts will go to this con variable And then we are able to assign account phone into that contact home phone This way it is working Right so this is all about today's session So I just planned all these scenarios And if you want to practice So you can just go through on this practice sheet Here five more use cases are available along with reference recordings So just implement and tomorrow we will be doing a delete and undelete And so if we see the session tracker So tomorrow we will be doing this delete undelete part And next week this will be completed Trigger portion And after that week we will pick epics test classes Okay so this is all about before update and after update So do as much practice as you can and please follow all the sessions If you want to understand it clearly Thank you so much for joining today's session And today I am having little bit cold and cough So I am just wrapping it early Okay Jana is asking to show the code So let me just show it one more time Rajeev we cannot use con list instead of ACC.context Because con list is blank above Con list is blank In con list we are just adding the updated contact The actual value of contact is available with this context list That is associated with that account Okay so this way this code is working So just practice it so that you will be able to understand it more clearly Okay so thank you so much We will see you tomorrow with three more events Before delete after delete and after undelete Thank you so much Bye everyone see you tomorrow