 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is my website studysalesforce.com. So here you can find links of all the videos that I created on Salesforce. So you can visit that. So in this video, I'm going to demonstrate the difference between before insert and after insert in Apex trigger. So in my previous video, I gave you the introduction of trigger and I gave you simple example of before insert. So in this example, you will see like how before insert and after insert works in case of Apex trigger. So we have scenarios for both. So first I'm going to implement the before insert logic and then after insert, right? So before insert scenario is if account industry is not null. So on account, we have one pick list that is industry. If it is not null and having value as media, then we need to populate rating as hot. So based on industry, we are going to populate rating field as hot value. So that should be done before insert, right? So let me first implement this logic and then we will move to after insert. So I'm moving to the org. So in our previous video, we implemented trigger on account. So I'm going to use before insert and after insert. So let me use both of these events here, but right now we are implementing before only. So I'm calling that method and later on I will be adding the after method as well. And this I am removing because we are not working on update in this video. So this video is covering only two parts before insert and after insert. Then here we are using context variable trigger dot is insert. Then if it is true, then it will be checking trigger dot is before. So in case of is before this before insert will be called then this trigger dot new will be passing the new list that will be available. So in this trigger handler, we can fetch that trigger dot new list into new list. So name can be anything here. Then you need to you need to iterate that list. So ACC will be having records. So now here we need to apply if condition. So I'm writing if ACC dot industry not equals to null and ACC dot industry double equals to media. So the condition is true. In that case, I need to populate hold thought in a rating field. So I'm just modifying this. So ACC dot rating. And here I am writing hot. Apply conditions in case of before trigger. So whenever you are going to create any account record and you are populating industry, and that is equals to media. So you can set, sorry, this trigger will set in the rating field hot automatically. So now I'm saving this. So here you can see our name of classes account trigger handler. So that is already written here name of method is before insert so that we are using here and this trigger dot new will be passing the new list into this new list, and we are iterating that's this. So if you are in creating or you can you can say like inserting records through data loader or data input wizard, so multiple records will be inserted. So all those records will be available into new list and this loop will iterate them and accordingly rating will be set. And let's say if you want to assign else, like for media rating will be hot and for others for others AC dot rating will be let's say cool. Right. So this way you can apply the condition so right now I'm not using this part I'm just showing you this if condition only so again I'm saving this saving this trigger as well. Now I'm going to account clicking on new. So I'm naming it as test. Three. And I need to find out industry. So I am searching for media. Clicking on save. So record is created and in details you can see our media is industry and rating is by default available as hot because of that trigger. Right now if I try to create one more record. Let's say test for four and this time I'm not populating industry or let's say I'm populating any other value other than media. So in that case nothing will happen. Nothing means rating will will not be having any value. So if you want to assign any other value into rating. So in that case you can apply else condition in this trigger handler class and accordingly you can populate the value. So this way without any dml operation we can use before events so that we can change the value of a particular field before saving the record. Right so we don't require any ID of the record here. So remember one thing whenever before saving of the record you want to update any field. So in that case you can use before events. So in this case we are just inserting the record. So that's why we are using before insert even and later on in other video I will be demonstrating you like how we can use before update and before delete as well. So this first scenario is covered. Now I'm going to implement the second scenario that is for after insert. So here requirement is we need to create related opportunity when account is created. So if you are creating a new net new account. So after account creation one opportunity should be created and that should be linked with that account automatically. So for this purpose we need ID of the account. Right. So whenever you are creating any record and you need a ID of that record as well. So in that case you need to use after insert right you can you cannot use before insert in that case. And we will be applying the ML as well like insert opportunity because those will be created as new so that those two things can be performed in after insert. So now I'm going to modify this so here before insert is already available before sorry after insert is also available and if else are available. So this time what I'm going to do. I'm copying this and I'm writing it as after insert right and here I'm going to apply one implement one more method public static void of the insert then list of account new list. And here I will be implementing the code for that. So in one trigger hinder class before insert method is available. So name can be anything like right so let's say we can write update rating. So if you're writing this name so you need to use this name here. Then here we can write create related opportunities. So we can copy it and we can paste it here. So this way method name will depend upon the requirement as per your assignments or projects. So here now new list is available and this method will be running in after insert context right so in that case this trigger dot new will be having ID as well. So whenever we will be rating this new list so we will be having the idea of each account. So through that we can create new opportunities so first time going to create list of opportunity. So let's say opportunity to be inserted equals to new list of opportunity. So I hope you remember like whenever we want to perform any dml operation so those can be done only on a list. So that's why we are creating list. So it is better to perform one dml for all the records that you are going to insert. So we will be creating as many records as accounts are created and all those opportunity record will be added into one list and then through one dml the those records will be inserted. So now I'm going to apply a new. New list will be providing account records one by one into ACC. And here I'm going to create opportunity records so. So I'm naming it as ACC dot name, so opportunity name will be same as account name, then opportunity dot account ID equals to ACC dot ID. So we need to link both account and opportunity so that opportunity will be available in the related list of account. So with this statement, both will be linked now OPP dot stage is prospecting and opportunity dot closed it. So all these are required feed except account ID so name is required stage name is required closed it is required. So that's why we are populating all these. So here I am putting system dot today. Right. So this way you can see this opportunity variable will be created now after creation of this we can add this into this list. So you can copy this name then dot add and OPP. So one by one opportunity will be created and that will be added into this list. Now after this loop, you can apply if condition if not list of opportunity is empty. So if it is not empty, then you can apply insert and this list. So this way you can create this method for after insert and this will be creating related opportunity for accounts those you are inserting. So now I'm saving this. So I hope you understood the difference like in before insert in before insert we don't require ID of the newly created account we just want to update some of the fields based on some condition. So that we can do in before insert but in case of after insert or you can say if you require the newly created record ID. So that you can fetch only when record is saved in the database. Right. So that we can. So that ID we can fetch into after insert event. So in creation of opportunity we require newly created account ID as well. So that's why we implemented this method in after insert context. Right. So now I'm saving this trigger as well. So as I explained in my previous video that whenever you create any trigger so by default it is active. So in setup you can search apx trigger. So all triggers will be listed here and you can click on edit. And if you want to deactivate any trigger so you can uncheck this checkbox. Right. So this way you can see all the code which we implemented on developer console is also available here. Right. So this way you can deactivate your triggers as well. So now in this scenario you can see both before insert and after insert operations are available. Right. So these both can be performed simultaneously as well. So let's say if I'm creating one opportunity. Sorry. If I'm creating any account naming it as test trigger five and I'm putting industry as media. So first before trigger will be executed then record will be saved and then after trigger will be executed. Right. So before insert will set rating as hot and after insert will be creating a related opportunity automatically and opportunity name will be test trigger five. So I'm clicking on save. So this account is created test trigger five. So if I move to detail so you can see rating is hot and if I move to related so here you can see opportunities created and its name is test trigger five. If I move to details so here you can see close date is available stages available account is linked with this. So this way both before insert and after insert are working properly. And I hope you understood how we can implement both the things together with the help of trigger and trigger handler class. So I hope you understood both the scenarios that I demonstrated in this video. So in my next video I will be demonstrating the difference between before update and after update. And if you want to watch more Salesforce related videos so you can find the playlist in my YouTube channel as well as on this website study Salesforce.com. So watch all videos related to trigger so that you can understand the different scenarios. Thank you for watching this video.