 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is website studiesalesforce.com. So here you can find all the videos on Salesforce. Those are created by me. In this video, we are going to discuss about after undelete operation in Apex River. And this is the scenario. So in my previous video, I demonstrated you before delete and after delete operation. So in continuation with that, so we are going to understand after undelete operation. So if you have not watched that video, so I will prefer like first you go through with before delete and after delete, then you can easily understand after and delete operation. So scenario is we need to undelete employee record that we are we were trying to delete and performing before delete and after delete operations. So if that record is deleted and then we undelete that employee record. So we need to set the active field as true. So requirement was when we were trying to delete the employee record. So first we need to uncheck or make active field as false then we were able to delete that. But when we were we are undeleting that record so we need to set that as true. And later on, I will also tell you like how we can maintain that left employee count field as decremented. So in previous video, sorry, in previous video I showed you that like if we open any and any account record. And if we go to the details so we have this field left employee count so we if we delete the record employee record. So this count is incremented. Now if we are trying to undelete the employee record so this should be decreased, right. So now moving to employer trigger. So here I use this after undelete event. Then this if condition is checking is after and is undelete and this method will be executed and delete employees. In case of undelete we have trigger dot new right so this you need to remember and here I have implemented a simple method. So first I'm going to show you like whenever you delete any employee record so we mark that active checkbox as false. But if we are undeleting that employee record so we need to mark that checkbox as true again. As this undelete performs in after so we need to perform dml operation so that we can update the records. So new list is available here. I'm iterating that list. Storing the EMP ID into this E variable and marking active as true. Then storing this into this list and after completion of iteration. I'm checking this list if it is not empty then I'm updating the employer records. Right, so this is a simple logic now moving to account. So here you can see I'm opening this test record. Right, so here if we move to the details so here you can see left employer count is one so I already deleted one employer call so that's why it is one. Now, here you can see this employer card is available. I am opening it. So it is active. So I'm marking it as inactive clicking on save deleting this. So as after delete trigger works so on details. If I refresh this, you can see left employee count is now two. And if I see the related so you can see deleted employer record is not available. So now you have two options either you can go to recycle bin and you can delete the record. Otherwise you can use the developer console anonymous window, right. So if we try to go to recycle bin so here you can search for recycle bin, and you can open it. So here all the deleted records will be available. So right now we deleted this 36 right. So what we need to do, we need to select this record, and we need to click on restore. So this record will be undeleted. Right, so. Okay, so if I click on this restore. Okay, before restoring this, let me show you how you can undelete from the anonymous window. From here you can query the employer record where account ID is this. So this I am hard coding, because we need specific employer record should be undeleted. And here you need to pass all those right, and then you can run and delete MP list. So let's try from here. I am clicking on restore. So here you can see 0036 not available now moving to accounts. So we deleted it from test. So on related list you can see this 0036 is available. So while deleted we marked it as active as false now you can see it is automatically true. Okay, this trigger is working fine and this method is working properly. Now, if I go to the details of this account. Sorry, I need to go to account. And if I move to the details. So still you will see left employee count is two. So now my requirement is I need to make it one because I undeleted one record. The employer record is deleted. So it will be incremented by one. Otherwise, it will, in case of undeleted, it should be decremented by one. So now what you can do you can copy the code that is available here. So this is the code which is related to after delete. So we can use the this code as it is. So let me use this code. So I'm putting it here. And yeah, so we need to update employee as well and account as well because accounts are related to that. And here we need to apply the condition. So we need to update each and every employee because whenever employees undeleted so we need to mark the record as true. Now here I'm going to apply one condition if EMP dot account is not equals to none. It means the undeleted employee is related to any console that we can decrease that left count by one. So for that I'm copying this and I'm pasting it here. So we need to update employee as well as we need to update account. So now I can use this here dot add EMP dot account IDs, right. And whatever IDs will be available here accordingly we will be querying accounts. Now, for selected employees, we need to iterate this. So I'm copying this as well EMP list. So pasting it here. Then using it here EMP list dot add EMP. So account records will be varied from here. We are going to use this map so I think I'm not copied that. So I need to copy that map as well. Okay, so I'm copying this whole because this account map will also be required. So pasting it here. So this is the account ID account map and this is employee list and this is the account map. Now everything is available here logic will be seen here you need to use minus one in place of plus one. And rest of the things will be same. So here you can see this will be updating the account and this will be updating the employer cards. So now I'm saving this. So let me try this thing so that we can understand it. So I'm opening one account ACC EMP. Sorry, ACC EMP one. And if we go to details. So in my previous video I created two employer record and I deleted so that's why this delete count is two and in the related list you can see employees are zero. Right, so now I'm going to use this account ID. And I'm going to use this. Right, so I'm just wearing from all rows. The employees which are related to this account and then I will be undulating them. So when I will be undulating them so each employee record will be having active checkbox as true and the related account will be decremented by the undiluted count of employee records. So it is saved. It is also saved now I'm going to execute this. So let me check this again. So now I'm going to execute this. So it is executed debug log is open so I'm just closing this because it is not required. So I am refreshing this account. So you will see in the related list to employees will be available with employer records will be available because we deleted to employer records from this account. So here you can see 34 and 35 are available so I'm opening them. Meanwhile, I'm opening this detail section and refreshing the account so that we can check the left employer account value as well. So in the details you can see this employee record is active marked as active. This is also at 35 and 34 both are active. Right, this is because in our trigger be implemented this thing on each employee we are assigning active as true. And for updating the account left count we are using the rest of the code that I copied from the above after delete. Right, so it is being refreshed. So here you can see left employer count is now zero because two records are undiluted so it is now moving back to zero. Now if I again delete those two so it will be two again and accordingly it will work. Right, so again you can see here. So all three scenarios are now covered before delete after delete and after and delete. And this is the before delete code. Then we have after delete code so we have we are filtering and then this code is implemented and this is the undiluted one. So in undiluted. First we are checking. First we are assigning true in active of employee then we are checking whether they are related to account or not. If they are related then we are creating set an employee list. Accordingly we are carrying accounts then through employee list. We are decrementing the count by one then updating accounts and employees. So this is the complete code that you can implement. Right, and if you want these codes so you can email me so my email ID is available on my YouTube channel as well as on the website link that I mentioned you on the homepage you can find the email ID. So if you need this code so email me so that I can send the code to you. Okay, so I hope you understood how undiluted is working. If you want to watch more videos so on this website you can find the links of those videos related to Salesforce created by me. Thank you for watching this video.