 Hello, friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is my website where you can find all the Salesforce video links together. So those videos are created by me and you can watch them to learn Salesforce. In this video, I'm going to demonstrate like how you can create roll-up summary if you have lookup relationship. So using Apex trigger, we can do that. So in case of master detail, we already have roll-up summary field, but in case of lookup, that field doesn't work. So we can create a custom solution for that. So the problem is, so this is a real-time scenario that you might need in your projects as well. So whenever an opportunity is created, updated and deleted, then we need to roll up the opportunity amount to accounts and will revenue. So whenever we create any opportunity, so we can link that opportunity with an account. So if opportunity is looking to any account, so in that case, we need to add the annual, sorry, we need to add the amount to that annual revenue field that is available on the account, right? So this is our requirement. So here you can see this is account. And on account, we have this annual revenue field. And on opportunity, if we see, so on opportunity, you can see this amount field is available. And if I open opportunity, so you can see opportunity account shares lookup relationship, right? So now let me explain the code that you can implement. So there will be three scenarios, insertion, updation and deletion. In all the cases, you need to roll up the amount to accounts and will revenue. So this trigger should be written on opportunity because whenever you are inserting, updating, or deleting any opportunity, then this operation should be performed. So all three events are available here. Then I check trigger dot is after, then in insert, I'm calling on after insert. Then in update, I'm calling on after update and similarly after delete. While we insert records, so trigger dot new is available. Then on updation trigger dot new and old map and deletion trigger dot old. So this is trigger and through this trigger I'm calling these three methods. Those are defined in this class, opportunity trigger handler. So this is the class where I implemented all the code. So here you can see on after insert method is defined. So it is starting from here till here. So it is receiving new list. Then I'm iterating new list and fetching opportunities one by one in OPP. And if OPP dot account ID is not null, then I'm fetching the account ID into this ACC ID set, right? So this is created here. Then I'm calling this method calculate annual revenue and passing account IDs. And this method will be returning accounts to be updated. So we need to check if this list is not empty, then we need to update the account, right? So this is the first method that will work on after insert. Then similarly I created one more method for after update. So here you can see we have new list and old map. Then iterating new list, checking if OPP dot account ID is not null and amount is changed. So this OPP dot amount is new version of the amount. And through old map we are getting the existing value if both are mismatched. So that we can have the account ID, right? And rest of the things are same as we saw in insert. So we are calling this method calculate annual revenue and passing the account IDs. Then third method is on after delete. It is also same but the only difference is we are having old list instead of new list. So you need to reiterate that and if opportunity account ID is not null, then you can get the account IDs and you can pass those IDs into this method calculate annual revenue. And if the return list is not empty, then you can update the accounts, right? So this way three methods are collecting account IDs. Now the main method comes here, which is calculate annual revenue, which is receiving account IDs. So here I created one list accounts to be updated and a map account ID to annual revenue, which is of ID and decimal type. And here you can see this for loop. So it is important. So this for loop is doing what it is doing. It is querying all the opportunities where account ID is available in this set, right? So we already collected all the account IDs. Those are related to the opportunities. So if the opportunity is available, sorry, opportunity account ID is available in this set. So we are querying those opportunities so that we can collect the rule of value. So now here comes the map. So if map is containing the account ID, right? That is on available on opportunity. So in that case, we need to get that value. So first time this if condition will be false because map is empty because we are having first opportunity. So this map obviously it will be empty. So now here you can see at line number 55 total is initialized with zero. And if opportunity amount is not null, then we are adding up opportunity amount to total and we are putting account ID and total into this map. So if it is not null, then amount will be added. Otherwise, total is already initialized with zero. So zero will be stored here, right? Then let's say we have one more opportunity that is related to same account. So in that case, a second time that this map will be having the account ID, right? Because either this if was true, otherwise total as zero was put in this map along with account ID. So if second time this account ID is available, so we are fetching the value, the calculated value that is available in the map. So it may be zero or it may be some amount. Then if the second opportunity amount is not null, then that total will be updated. And then again, along with account ID, we are updating the map, right? So map has a nature if we put same key again and again, so latest value will be available in the map. Previous one will be erased. So this way this map will be having account IDs and respective total of all the related opportunities, right? So after completion of this loop, after completion of this loop, we need to check the map if map is not empty. It means it is having some values. So we need to iterate it based on the keys. So we are fetching key set, then creating account instance, then putting the ID through this I variable, putting the revenue by getting the value of a particular key in this ACC instance and adding that into this list. And then returning this list. So this list will be returned here in the delete, then it will be checked whether it is empty or not. Accordingly, it will be updated. Same with the case of after update and same with the case of after insert. So this way, the common method is working for all the insert update and delete methods. So now we need to test it whether it is working fine or not. So let's say I have one account. So ACC EMP one, right? So I am opening this one and ACC EMP. So I have opened two accounts. So here you can see opportunities already available, but amount is null. So annual revenue is already null here. Right. And ACC EMP one. So it is having two opportunities. One is having amount as 5000. One is blank. So annual revenue here you can see it is 5000. Right. So now I'm moving here, opening anonymous window. So here you can see opportunity one I'm creating. Name is test 11 stays date. And this account ID. So this is the account ID of this ACC EMP one. So for that account, I'm creating two opportunities. Let me create two more. So OPP three. So these I will be creating under another account. So that we can see bulk roll up. Then four. So now it is done. And here I need to update these as well. So I need to add OPP three and four. And here I need to update the account ID as well. So from here I'm copying this. Putting here and putting here. So this way total four opportunities will be created. Two under one account and two under another account and for one account total should be 15,000 and for other total should be 15,000. But as you can see 5000 is already available. So it should be 20,000. Here it is null. So it will be 15,000. So now let's perform this operation. Execute highlighted. Okay. So one mistake here I need to write insert. So now I'm refreshing this one. I'm refreshing this one. So on this one you can see 15,000 is available. And two more opportunities are created. One was existing and two are created here. Right. And remember one thing I use this null here. If existing amount existing opportunity amount is null. So that should not be calculated. If we commend this and we directly use this OPP dot amount. So null pointer exception will be there. So this you need to remember. And here you can see 20,000 is available because now this account is having four opportunities. So 5000 was earlier one and then five and 10,000. To be created. And one is having null. So this way in session is working fine. And let's do one more operation. So now we need to update the opportunities. So for that we require ID. So I need to put ID here. And I need to put ID here. So I'm going to update one, one opportunity for each of the account. So I'm going to do a test to make sure that I'm not wrong. So which I'm updating 11. So test 11. Opportunity I'm updating. So here it is. So I'm opening this one. And from here also I'm opening this one. So it is related to. ACC. EMP. So I'm copying this. And pasting it here. Then I'm copying this. it here. So now I need to update OPP 1 and OPP 3. So I am removing this, I am removing this, this as well, this as well, right? And instead of insert, I am using update and I am making these as 50,000 both, right? So now selecting these and clicking on execute highlighted. So again, I am doing a refresh. So you will see rollup, rollup will be changed like we updated two opportunities on two separate accounts. So this I am showing like bulk operation will also be performing well. So you can update or insert or delete from UI as well. So that we can also do. So here you can see it is 60,000. So you can calculate the some fifth. So the opportunity which was earlier having 5000 we made it 50,000 and 10,000. So 60,000 is annual revenue and here 65,000 because 550 and 10. So it is 65,000. So this way it is working fine. So I was telling you from UI also you can update the opportunities. Right? But in that case, you will be able to update only one opportunity. So that's why through developer console, I'm showing you how you can perform bulk operations. So let me show you a deletion from UI. Right? So this test 11 is having 50,000. So let me delete this one. So it is deleted. Now I'm repression this account. So you will see 60,000 will become 50,000. So these operations you can perform from UI as well or you can perform through developer console or you can perform through data loader or data import wizard as well. So here you can see annual revenue is now 10,000 because one opportunity is deleted. So that's why rollup is all automatically updated. So this way I hope you understood how we can perform rollup summary operation on lookup relationship with this example. So keep in mind, you need to cover all three scenarios, insert, update and delete because in case of rollup, if you insert, then also parents should be updated. If you perform update on that specifically, then also rollup should work. And if you delete any child, then also on parent, updation should reflect. So this way I explained you the total one and if you want to perform rollup summary based on count, max, min, so other operations that you can also apply here. So you need to modify this logic that is implemented in this method, calculate annual revenue. So I hope this will help you in your projects so that you can implement rollup summary on lookup relationship. So I hope you understood whatever I demonstrated you. And if you want to watch more Salesforce related videos, so you can go to this website and you can find various video links. Those are created by me. Thank you for watching this video.