 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So this is my website where you can find all the video links related to Salesforce. Those are created by me. In this video, I'm going to demonstrate you how we can use after insert, after update and after delete together in Apex trigger. So let's understand the scenario that we are going to use. Common scenario is there like if opportunity is created and if amount is not null, then we need to add amount to accounts annual revenue field. So this is on insert. Then if we are updating any opportunity and we are updating the amount so that needs to be updated to accounts annual revenue too. And if we are deleting any opportunity, so we need to update the annual revenue accordingly because if opportunity is deleted so that amount should be deducted from the annual revenue. Right. So to implement this scenario. Let's see this. So this is account record and on account we have this annual revenue field and we need to sum up all the related opportunities amount. Right. So for this, I already created trigger. So I created trigger on opportunity because this should be performed on creation, updation and deletion of opportunity. So after insert after update and after delete events are available here, then these are context variable. So if trigger dot is after is true, then if is inserted is true. So this on after insert method will be called. So I have created separate handler class for this trigger that is opportunity trigger handler. So on creation we have new list so that that that is available in trigger dot new. So I'm passing it in this method on update we have new as well as old map so I'm passing both the parameters. And in case of delete, we have trigger dot cold. Now if I move to opportunity trigger handler so here you can see all three methods are available. So now we need to apply the logic. So first I'm going to apply the logic for after insert. So here, let's say I'm going to create a set of ID account IDs, then list off. So this I'm creating. Now I'm going to iterate this new list. So for opportunity. So now I need to identify the account ID through these new inserted opportunities. Right. So what I'm going to do, I'm going to add through this right so whatever opportunity you are creating. So if it is related to any account so we need to put the account ID so we can apply one if condition as well. So if OPP dot count ID not equals to none, then only it will be added. So there may be possibilities like if you're inserting any opportunity and that is not linked to any account so that will generate an error. So this way I created this set and after completion of this loop. I'm going to call a common method that will be used for insert update and delete for all. So calculate and will revenue and I'm passing this ACC ID set to this method. Right. And whenever this account to be updated list will be returned so I'm going to check if this list dot is empty. So if it is not empty, then update. Right. So showing some errors. So let's check the errors. Okay. Right. So this way you saw I created one set of IDs. So this set will be storing account IDs. Those are related to opportunities. Then I created a list of account that we are going to update, then opportunities are iterated. So if OPP dot account ID is not equals to null. Right. So if on opportunity account ID is not null, then that account ID will be available in this set. We are passing this set to this method so that we can calculate annual revenue and the updated list will be passed here and that will be processed to be okay. So why this error is coming because this method is right now not available. So let me create a block only. So list of account method name set of ID. So this method we are going to define later on. So first let me copy this. Yeah, I'm pasting it here. So this is for an after update. So new list and old map both are available. So I'm iterating new list. So if account ID is not null and now we need to check one more thing. So the amount that is available on opportunity that is updated or not. Right. So what I can do OPP dot amount not equals to old map dot get OPP dot ID dot. So if new list amount that we are accessing through OPP dot amount is not equals to old map. So old version amount and new version amount if both are not matching. In that case, the related account ID will be available in this set. And we're passing this into this method then same operation will be available here. Right. And now we need to perform this for deletion. So I'm going to implement that as well. So I'm pasting it again. So here we have cold list. So I'm going to iterate this cold list. And yeah, rest of the things will be same. If account ID is not null, then we need to put that account ID into this set and then we need to call this matter. So this way I implemented all three methods. Right. So they are finding the account related account IDs and through that we need to now calculate annual revenue. So now I'm going to implement this method. So this will be the countless in that we will be storing the data that we are going to update. So I'm going to create a map of ID, decimal. So account ID to annual revenue. So this is a map. So after this, now what I'm going to do now I'm going to query all the related opportunities with the account ID that we passed here, so that we can calculate annual revenue. So I'm iterating for OPP, then I'm writing SQL. So select ID amount from opportunity where account ID and ID. Right. So this way, all those opportunities will be carried where account ID is available in this set. Right. So we will be trading all the first opportunities and then we need to return this one. So I'm writing this as well. So we need to finally return this one. Right. And here we need to implement the logic. So now all the opportunities will be available into this OPP. So one by one we need to iterate and we need to calculate the annual revenue. So I'm creating one more variable decimal total equals to zero. Now if so we have this map. So we need to use a map name dot. Not get, let's say contains key. And key will be OPP dot account ID. Right. So if this map is containing the account ID, it means that account is already available in that map. So what we can do we can put total equals to this map dot get OPP dot account ID. Right. So if opportunities related account ID is already available in this map. So it's that maps value will be available into this total. And further what we can do. So further we need to calculate total equals to total plus and the amount that is available on opportunities. So OPP dot amount. Right. So first time whenever opportunity will be available. So the account ID won't be available in this map. So total will be zero. So zero will be added to this opportunity amount. So what we can do, we can do one more thing. We can apply if OPP dot amount is not equals to null. In that case, only this total will be calculated. So this also you can do so null pointer exception can be resolved. Now we have total value. So what we are going to do we need to put this in this map. So first time this if won't be executed. So total will be zero. So zero will be added to this opportunity amount and that will be stored in this. Right. So the output OPP dot account ID comma total. Okay, so here you can see some errors are available. So the spelling was incorrect. So I need to write account ID so that was misplaced and more errors are coming like a non static method cannot be referenced from a static constant context. So, yeah, public static. So now those errors will be resolved. So now what you see this map will be having account ID in the total after completion of this loop. So now we need to iterate this map. So checking if it is having some values or not. So if this map is not empty, then we can iterate this map so for ACC dot ID equals to ID. Let's say I, and the map name dog key set. Now, on ACC equals to new account. Then ACC dot ID equals to I and ACC dot annual revenue equals to now we need to use a map dot get. So in map on this key whatever values available that we go to this ACC that instance that we created, and we need to put this into the map. So here is the map for the list into this list we need to add this ACC. Right. And after completion of this loop, this list will be returned to the above methods. On top you can see, while inserting we use this list so after calculation this method will be returning a list so that will be checked if it is not empty. So accounts will be updated and similarly for this after update, and similarly for after delete. And this is a common method for all three. So here we have an error. Okay, this should be accounted. So I'm mistakenly typing Dean instead of T. So he paid it should be correct. Otherwise, errors will be there. So now it is complete. So I'm saving this. This is already saved now moving to account. So, let's check the related. So it is having one opportunity already. And if I open this opportunity, and if I check the details so you can see amount is blank. Okay, so let me use this account ID. I'm using this account ID. And I'm going to use this anonymous window to create opportunities to create update and delete so that we can apply this on bulk as well. So what I'm going to do, opportunity equals to new opportunity. OPP one name equals to test one OPP dot state name equals to aspect. This is as well. Yeah, it is prospecting and OPP dot close it equals to system. Right and OPP dot equals to so I'm copying this. So for testing purpose, I am hard coding this. So this is first opportunity. I'm copying this creating one more. Right. So now I'm going to create a list so that we can perform bulk operation. So opportunity list of opportunity OPP list equals to a new list of right. So now in this I'm going to add these OPP one OPP to insert OPP list. Right. So right now you can see amount is also not there. So let's put that as well. Now amount equals to 1000 and OPP dot amount equals to 2001 more mistake I did. I used OPP instead of OPP one and here OPP instead of OPP two. Okay, now everything is ready. So I can execute these. Right. I'm going to open the lock so I'm executing. Okay, so this is showing an error. Okay. So what is the error? It is saying I queried SOQ without querying account ID because I'm using that here. Right. So I need to use account ID here as well. So whatever field we use and process so we need to query that as well. I execute this so it will be executed successfully. I am doing a refresh. So you will see two more opportunities will be related to this account. Let's check the related list. So here you can see total three opportunities are available. So here amount is 2000 and here amount is 1000 and earlier one was zero. Right. And we also applied null check. So let's check the details. So here you can see annual revenue is 3000. Right. Now let me update these. So I'm updating this one to 5000. Right. So now total should be 7000. So update. Okay, let me update both. So update OPP list. Execute. Okay. It is saying ID not provided. So for updation, we need to provide the ID as well. Okay. So OPP 1.ID equals 2. So here one opportunity ID I need to provide that I'm going to update. So these are the things that we need to remember whenever we want to perform any operation. So I'm fetching this, putting it here. Now I'm opening another one. Copying its ID here as well. So I'm now executing. So now it executed successfully. So what was the values? 3000, 5000. So now let's check. So I'm refreshing. So opportunity amount will also be updated and the annual revenue on account will also be updated. So here you can see annual revenue is 8000. And on the related opportunity you can see 5000 and 3000 is there. So in case of updation also it is working fine. Now it's time to delete. So let's say delete OPP 1. I'm just deleting one opportunity. So I just want to execute this. Okay. For that, let me query this opportunity for deletion here. So let ID, opportunity ID equals 2. Let's say this one and delete OPP. So these two statements I'm going to execute. So what I need to do, I need to click on this execute highlighted by mistake I was clicking on execute. So I'm clicking on execute highlighted. So now it is deleted. So if we click on execute, so all the statements are executed. So this also we need to remember. So now I did a refresh on account. So you will see one opportunity is deleted and annual revenue is updated. So it is now 5000 and total two opportunities are available. So this way I hope you understood all three operations after insert, after update and after delete. So you can implement this code in your org as well. So initial after insert, after update and after delete code are somehow similar. And they are using this common method so that we can calculate total amount on annual revenue field that is available on account. So implement this in your org so that you can understand the process. So if you want to watch more Salesforce related videos so you can go to this website and you can find video links. Those are created by me. Thank you for watching this video.