 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is my website studiesalesforce.com. So here you can find links of all the videos together created by me on Salesforce. So in this video, we are going to discuss about before delete and after delete operations. Those are available in Apex Trigger. So first scenario is for before delete. So employee record cannot be deleted if active is true. So on employee, I have created employee object. I have created active field custom field that is Boolean field. If it is true, then employee record cannot be deleted and this operation will be done in before trigger. So whenever delete is performed on employee. So this should be performed. So let me first implement this one. So here you can see on employee custom object. I have created employee trigger which will run in delete context in before and this method will be executed check employee status. So right now I am commenting this one because this is for after operation or you can say after delete. So in case of before and after delete, we need to pass trigger dot old variable. So new is not available because whenever we delete any records so it automatically will be available in trigger dot old. So you can see in both the cases we are passing trigger dot old and in the handler, I'm going to patch this into old list parameter will be received here. And whenever you are going to implement trigger. So this is the best practice in trigger. You just need to call the methods those will be available in the trigger handler classes and in the class you can implement the actual logic. So now all the list is iterated one by one employee record will be available in EMP and if active is true then this error message will be displayed. So now if I move here, go to employee record. So here you can see this active checkbox is true. So now if I try to delete this. So an error message will be available and active employee cannot be removed. And if I uncheck this. Save this. Now if I try to delete so this record will be deleted. And so this way you can perform some operations by performing delete operation in with the help of before delete. Now next is after delete. So for this we have scenario. So when employee record is deleted then update left employee account on account so you need to assume that our employee records are looking up to account so they share lookup relationships account is on parent side and employees on child side. Whenever any employee record is deleted so we need to count like how many records are till now deleted from a particular account. So that will be available in left employee account variable. So now, if we move to the trigger so here you can see in same trigger handler class we have one more method than one more method that is update left employee account on account. And again we are passing trigger dot hold. So here if you see this method is available. It is receiving oldest. And these are some variables that we are going to use. So first what we are going to do, we are going to filter the old list, so that we can identify those employee records, which are looking up to account only. So if on employee account field is not equals to null, then account IDs will be available into the set that is created here and employee record will be added to this list EMP list that is created here. So after filtering, if this ID set is not empty then we need to query accounts. So we are having ID and left employee account value and we are ID in this account IDs. So the account IDs that we are filtering those account we are querying and they will be available into this map. Now if list is not empty, it means we have certain employee records, those are deleted and they are looking up to account as well. So we are iterating that list. Now, if we now we are checking this map that we queried, which is having accounts. So if the deleted employee records account IDs available in this map, then we are performing this operation. So this is bit tricky, like you need to understand it. If we are deleting only one employer record that is looking up to account. So then we can perform simple operation like we can add the existing left employee account value by one. But if we are deleting five employer records in bulk and those five employer records are looking up to same account. So in that case, in the code we need to identify like five employer records are deleted and we need to increment this plus one by five times. Right. So what we are doing we are maintaining a map account ID to total count. So at top you can see this map is of type is its key is ID and values of type decimal. So first we are checking whether it is having any account ID or not. So initially it will be blank. So contains key will be false. So this else part will be executed. So we are putting account ID as key that is on employer record. And through this map, we are fetching the current left employee account value that is available on the account. And for this first deleted employee we are incrementing it by one, and it is we are storing it in the local map. Right. Let's say next employee record will be available from this list. That is also looking up to same account and that is available in this map. Then again we are checking this local count map, whether that employee account key is available or not. So this time it will be true. This second time the employer record which we are deleting it is looking up to same account that was looking up by the previous employer record that we traversed. So we are fetching the count value. So now this map is having the count value and we are fetching it through get incrementing it by one storing into count and then again we are updating the map. So we are putting that account ID and the updated count value. Right. So this way, if same employer records will be available, those are looking to same account. So this map will be maintaining the values. So he will be account ID and the current left count value will be available in this count and will be available in this map. So in the condition of this process, we need to iterate the map account map. So through key set we will be trading and account ID will be stored in this ACC and the left count will be get through map and stored here. Right. So updated count will be available and then this list will be updated. So this is the list ACC account to be updated list. So if it is not null then account records will be updated. So this way, whenever an employer record is deleted or bulk in bulk you are going to delete some employer records. So the related accounts will be updated on this field left employee account. So now I'm going to take two account records. And we are going to delete the related employer records. Right. And then we will see what will happen on the account object that this left employee account field. So I hope you understood this code. So maybe you can implement this code in your org. So one by one, you can type each and every line. And then you can understand like line by line what is happening in this code. So I'm scrolling it slowly so that you can by pausing this video, you can write the code in your org. Now I'm moving to the org and let me pick this account ACC EMP. So if we go to details. So here, we need to check the value of that field so here you can see it is already having four as left employee account. It means I already deleted four records related to this as employer. And right now it is not having any related employer. So now I'm going to create two related employer for this. This is the ID and designation. Let's say that one clicking on save. So one employer cord is created. Then creating one more to write. So two employer cards are created for this account. Now, so its name is ACC EMP. Right. Now I'm going to create one more account record and I'm naming it as ACC EMP one. Right. And I'm clicking on save. So the code is created. Now I'm moving to details. So here you can see by default its value is zero because whenever I created this field, so I made a default value as zero. Otherwise, we need to also check this in the code like if it is not zero or null, then we need to initialize it first and then we can add the values otherwise null pointer exception will be thrown. So I'm creating first employee. So it is there three. Now I'm going to create one more employer. So it is there for. So now we have two accounts ACC EMP and ACC EMP one. So on ACC EMP left count is already four and on ACC EMP one left count is zero. Right. Now I'm moving to developer console opening anonymous window. And now here I'm going to query employer records and then in bulk I will be deleting them. So let me first save it. So I'm saving the trigger so that it will run properly. Now, the list of employer EMP list equals to select ID employer their account underscore underscore C equals to ACC EMP. And this is first condition or C EMP one. Right. So let me just check it with debug system dot debug EMP list. Right. So I'm just executing this opening the debug log. Okay. There's an error. So it executed I forgot to mention colon sign. So you will show the result in debug. Okay, debug is blank. So there's some mistake. Okay, okay. So I'm just matching it with name but I need to match it with ID. Right. So this is incorrect. So I'm picking the ID because IDs will be available there. So this or the another one. ACC EMP. So for demo purpose I am picking this ID because in real time we don't use these IDs. So why I am doing so so that I can show you how we can perform bulk deletion. Otherwise we would have deleted those employees one by one from the UI. So in that case you won't be seeing the case like if we are deleting bulk record so how that will be working. So I'm going to execute this. So this time it will fetch the records. Yeah, here you can see. So two records are available here and to total four records are available. Right. So the result is correct because we have four, four. So for this account we have two employer records related and for this account also we have four, sorry, two employees related. So now what I'm going to do I'm going to perform delete operation on this EMP list. Right. So total four records will be deleted in bulk. So I'm executing this. Yeah. Okay. So delete operation performed. So now I'm refreshing it. So from UI we will be checking the left employer count field. What will be the value of this. So in case of this ACC EMP earlier it was four. So now it should be six. So here you can see it as six. And if we move to related. So here you can see no employer record is associated. And if I open ACC EMP one, so you will see. Employee are not related and in details you can see left employer count is two. So this way, after performing bulk deletion this code is working fine. So this way I hope by practicing you will understand this logic as well. So initial value will be available from here. So it will be executing one time only whenever first employer record will be available that is looking up to one account and another employer record will be available that is looking to same account in that case this condition will be true and accordingly results will be calculated. Right. So this way I hope you understood both before before delete and after delete operations. So I also demonstrated you like how we can perform bulk deletion operation so this you can perform as well for insertion and ablation as well. So from anonymous window you can try that or from UI also. So this is my website study Salesforce.com so here all the links related to Salesforce videos created by me are available so you can visit that and learn Salesforce. Thank you for watching this video.