 Hello everyone. I'm Sanjay Gupta. Welcome to Sanjay Gupta Tech School. In this module, I'm going to demonstrate how to implement Schedule Apex along with its test class. Let's understand the scenario for which I'm going to implement Schedule Apex. So the scenario is create a remanded task on opportunity where opportunity is open and close date is less than today. And we need to ensure proper code coverage as well. So let's implement the scenario with the help of Schedule Apex. So here you can see I have already created one class known as opportunity reminder and it is implementing schedule label interface. So whenever you want to implement any class and you want to schedule that so you need to implement this schedule label interface. Now I'm going to implement the code so that you understand how it will work. So in this Schedule Apex, you need to implement execute method and here you need to pass schedule label context. And inside this method, I'm going to vary the data. So I'm creating list of opportunity. Now here I'm going to apply SOQL. So select ID, stays name, close date from opportunity where is closed equals to false. So is closed means opportunity. If opportunity is closed then is closed will be true. Otherwise its value will be false. Let me just copy and refresh it. So this is our first part of the condition. If is closed is false, it means opportunity is open and close date is less than today. So if these two conditions are matching only those opportunities will be queried and they will be available into OPP list. So now I'm going to create a list of tasks. So naming it as task list. Now I'm going to check this OPP list. If it is not empty, then I'm going to iterate a loop on this opportunity list. So opportunity OPP, and I'm iterating on OPP list. Okay, I'm going to create a task. Now we need to populate some fields of this task instance. So I'm just filling those. So task dot subject equals to, let's say, reminder task. Dot status equals to open TSK dot priority equals to normal TSK dot what ID equals to OPP dot ID. So what ID means like with which record this task will be related. So it will be related with opportunity. So that's why I am assigning OPP dot ID and task dot owner ID. So who will be the owner of this task. So I am assigning OPP dot owner ID. And now I am adding this task record into the task list. So this way for each opportunity task record will be created and after completion of this. I'm just checking if the task list is not empty. Then insert task list. So this way, our task will be created. So I'm going to save this so that we can execute it. Right, so if you want to execute this. So you need to write some code in the anonymous window. So I created instance of this OPP reminder class. Now I'm going to create a grown expression so that I can schedule it. So basically, to understand this grown expression, you can go with this comment. So here, seconds, minutes, hours, day of month, then month, day of week and optional year. So these this way these values are populated. Now, job ID equals to system dot schedule. Let's say create task, then schedule, and then instance of that reminder class. So if you if you execute these three statements of your class will be scheduled automatically and it will start execution. Right, so I'm just going to show you a pitch jobs. So I'm refreshing it. So here you can see there is no scheduled apex at top. It is already cute, which is for any other class. So now I'm going to execute this. I'm going to refresh it so you will see schedule apex will be cute here. Right, so at that specified time it will start execution, which is available here. So this way you can create your schedulable apex class with the help of schedulable interface, and you need to implement execute matter and there you can write the code. So it's time to implement its test class so that we can make sure the code coverage is 100%. So I'm going to create new apex class, naming it as opportunity reminder test. So first of all, I'm going to create a grown expression so public static string, grown expression equals to this is a grown expression. So I'm just cutting it and putting it here that I'm defining the method. And this method I need to create data. So I'm creating list of opportunity, I'm going to create a loop, so it will start from one, it will repeat five times. Five opportunities will be created. So, opportunity, OPP equals to new opportunity, OPP dot name equals to test plus I, OPP dot stage equals to respecting OPP dot close date equals to so now we need to put close date less than today. So I'm just using date dot today dot add days minus seven right so close date will be seven days from today. And now I'm going to add this opportunity into the opportunity list. So after completion of this loop, I can just insert this OPP list. These records will be inserted. Now, I just need to fetch the IDs of this OPP list. So what I'm going to do, I'm going to create a set of ID equals to new set of ID. I'm just iterating on this opportunity list. And here OPP IDs dot add OPP dot ID. So this way, opportunity IDs set will be created. Test dot start test, test dot stop test. Now in between these two lines, I'm going to schedule the job. So string job ID equals to system dot schedule, create task, then here I need to pass the grown expression. So I'm copying it and pasting and then I need to pass the instance of that OPP reminder class. So I'm just using new OPP reminder. So its instance will be created and it will be passed into this method. Now after this, so it will be executed. Now after this, I need to verify whether task created or not. So I will be creating related, I will be querying all the related tasks. So we created five opportunities. So five tasks will be created. So I will be matching that quantity. So for that, I'm going to query the data. Okay, so I'm just creating list of tasks, then applying SOQL, select ID task where what ID in OPP IDs. So here if you see in what ID we set opportunity ID. So I'm checking if task what ID is in this set. So those tasks will be queried. And now I can apply system dot equals. So it should be equals to five. So task list dot size should be equals to five. If it is equals to five, it means five tasks will be created for these five opportunities. So now I'm saving this code, opening this block and clicking on test. Now I'm clicking on run test. So this test class is executing and it failed. So let me check. Okay, so this Apex job name create task is already scheduled for execution. So basically when we executed it, so it is already scheduled that's why it is showing the error. So what you need to do, you just need to use this code so that you can abort the queued scheduled Apex. So here I just queried all the cron trigger jobs. So those will be aborted, right? But in reality, you won't be aborting all. So for demonstration purpose, I just aborted all so that we can run this test class, right? But you can search or you can Google it like how to abort any specific schedule Apex, right? So that is aborted. Now let me just run this test again. There's again failing. So let me just check what is the error now. Okay, so, okay, so we just queried without owner ID. Okay, so basically what we are doing in this class, we are using OPP dot owner ID and that owner ID I didn't query. So I just need to vary it here. So I am saving this class. Now I'm going to run this test class again. So you can see this time it is executing successfully. There is no error. So this assert is passing. And if I go here and click on this, so you can see 100% code coverage is there. So this way, with the help of this test class, I hope you understood how we can cover scheduled Apex with the help of test class. Thank you.