 Hello everyone, I am Sanjay Gupta, I welcome you on Sanjay Gupta Tech School. In this video I am going to demonstrate how you can prevent account record from being edited if the record is created 7 days back. So all the records which are created 7 days back, if you try to update them then there will be a error message displayed on the UI. So this is kind of custom validation that I am going to implement with the help of trigger and later I will be implementing the test class so that we can perform the code coverage of the code. So jumping to developer console. So here you can see I have an account trigger already open. So this account trigger I used to demonstrate many of the use cases. So here I am going to modify this update part because whenever record will be updated then only our code should run. So let's first implement the method here in this account trigger handler. So it will be public static void prevent update. So here we need to pass two parameters. So one will be list of account and another will be map of id, account, old map. So whenever we update any account or any record, so there will be two entities available. One is new list and one is old map. So from here we need to call the method and we will be passing those parameters. So if you are watching all the previous videos so I think you remember what is the use of trigger dot new and what is the use of trigger dot old map. So new contains the new state of the record that is being updated and old map stores the old state of the record that is being updated. And here I am writing prevent update that is name of the method. So whenever we will be updating so in case of before update that method will be called and trigger dot new and trigger dot old map will be passed. Now here what I am going to do, I am iterating on list so for account ACC then ACC list. Now we need to apply if condition so the record which we are going to update so we need to check its created date. So if created date is less than system dot today minus seven. So if created date is less than today minus seven days. So it means that record is created before seven days. So here we can put ACC dot add error and here I can write the message. So this is the message. So this message will be displayed if you are trying to update the records which are created seven days before. So this is kind of custom validation that will be displayed. So here you can see we are using updates still we are not able to use this because here we don't need to compare the old state and new state of that particular record. So what we can do, we can just remove this part. And from here also we can remove this part. So if old values are not required so you can just remove that part from the trigger and the trigger handler. So this way the code is implemented. Now if I go to the UI so I'm going to test it whether it is working or not. So here I'm going to add one field that is created date. So it is added. Now here you can see lots of records are available whose record created date is less than seven days. So if I open this record and try to update the record. So here you can see it is giving me error. You cannot update records created seven days back. And if we have any record that is created within seven days so I'm just sorting it. So there is no record which is created within seven days. So what I can do I can just create a new record. So if I try to update this record. So it is within seven days. So if I try to update this record. So I will be able to update this record successfully. So you can see. So this way our trigger is working fine. Now if you want to implement the code coverage for this piece of trigger handler method. So let's jump to trigger handler test class. And here we just need to create one record or bulk records account records. And then we will be updating those records. So here I'm going to copy this method and I'm pasting it. So here we need to test prevent update. So method name is prevent update update test. Then here I have created a list of account. Then here you can see a loop is iterating. This loop is iterating five times and it will be creating five account records. And those account records will be added to the list. Then to refresh the governor limits we are using start.test.starttest. And at the bottom you can see test.stoptest is available. Then account list is inserted. Right. Then this variable exception throne is available. So here some custom validation will be thrown. So some error message will be there. So we can just put try and catch blocks. So now here I'm going to update account. So I am just going to implement a loop. So for account ACC, colon ACC list. So I'm going to update phone value of this account of all the account records. And after completion of loop I'm just writing update ACC list. Right. Then we have this exception. So here in this exception like this variable we are setting is true. Then we are trying to fetch the error message. So here I'm going to store. So I just copied this value and I'm going to place it in place of this custom label. Okay. So here you can see I have this Boolean variable and I'm just going to split it into two lines so that it will be readable. So here exception will be available in this E variable. So this through this E variable we are getting the masses and checking whether it contains this masses or not. So if contains you cannot update records created seven days back. So it contains. So it will be returning through otherwise false. If it returns true, it means this assert will pass. Right. If it is returning false then this assert will be failing. Right. So after this we have another assert so which checks true with exception through. So if exception is thrown then it will be passed. If it is not thrown then it will be displaying this error message which is available here. Right. So this way this test class is implemented. So right now these records will be created in the within seven days range. So whenever we will be updating so this catch block won't be executed. So I'm just saving this code and I'm trying to run the test class. So I just want to run this prevent update test. So what I can do go to test and new run then account trigger handler. And here we can see we have lots of methods because I demonstrated lots of methods in. Other scenarios. So those are already implemented here. So we need to just run prevent update test time selecting it and I'm clicking on run. So only this method will be executed and it's results will be available here. So it is failing. So let's see why it is failing. So here it is showing sorry it is showing assertion failed. No exception was thrown. Right. So if you if you want if there is no exception like your code is working fine. So you can just comment it. Right. Because this is for testing purpose whether exception is throwing or not. So here exception is not thrown. So that is why this assertion is passing. Right. It is throwing an error message. So you can just comment because if exception is coming custom validation is working then it will be handled through this catch block. So now if I run this test class. So here you can see it is passing. Right. So this time all the methods I executed. So all are passing including that method which were failing earlier. So now if I go here and try to check the code coverage. So here you can see it is working fine and this particular line is not covered because here we are not able to create the record which is having created date for last seven days because in test class we are creating records in the current date. So today's date will be available here. So it is not possible to modify that created date so that it will be having records with last seven days. Right. So this way I hope you understood how we can implement the custom validation so that we can prevent updation of the record. So if you want to apply any other condition like if in place of this date you want to apply any other field that you can modify in the test class as well. Right. So that is also possible. So there are different conditions, different scenarios will be available. So this is just an example like if you want to prevent update on particular record those are created seven days before. So how you can do that? So this is it about the requirement. So this way I demonstrated you how to prevent account record from being edited if the record is created seven days back. Thank you.