 Hello everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today I'm going to deliver day 56 session and in today's session I will be covering scheduled Apex and yesterday I completed a QABLE Apex demo and like I didn't demo you how we can implement the test class. So in today's session I will show you how we can implement test class for QABLE Apex as well. Right. So today's session will be ending all the topics of Apex programming and from next week we'll be starting Visualforce. Right. And if you want to follow all the sessions so you can just have this session tracker. The session tracker link is available in the description of the video and from here like the Salesforce exercise sheet link you can access where all the exercises are available. Right. So day wise all live session recordings and practice exercises are available. So you can follow it so that you can improve your learning. Right. So moving forward if you have joined this session for the first time from the boot camp. So through this slide you can know about me. So I am running Sanjay Gupta Tech School for learning platform and I have total 16 plus years of experience and helping students, freshers, professionals to ram their career in the Salesforce ecosystem. So this is me who is delivering all the sessions. So so far I like this is day 56. So so far I have delivered 56 sessions to this beautiful community and from the beginning like I'm just requesting all the learners to follow all these best practices. So I hope everybody is following all these best practices. So moving forward like be consistent from next week onwards different different topics will be available in different weeks. So just join those sessions as well and do regular practice. Then you can join this telegram group where people are helping each other, whatever doubts they have. And today week 15 will be completed and from next week, like we'll be starting visual force, then we will be having two weeks for integration. Right. After that, everything will be for lightning component development. So we'll be discussing about prerequisite like HTML, CSS, JavaScript, then I will explain aura and LWC. But prior to that, I will be dedicating one week for a development project. So in that development project, I will be covering flow and apex related scenarios. So flow scenarios are already there in the project document. Apex trigger test classes as synchronous apex related scenarios I will be updating. Right. So just follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram and Telegram. If you want to receive timely notification and want to be connected with the platform and all the important links are available in each video description. So moving forward, so follow this YouTube channel to the needy person who want to learn things in easy way. And now I'm jumping to the curable Apex demo. So like yesterday, what we did, we implemented curable Apex class. So this was the example and now I'm going to implement its test class so that you can understand. So creating file new apex class and naming it as update parent account test. Right. So first of all, what we need to do, we need to create the data. So I'm just annotating it with at the rate is test. And here I'm going to implement the test method. Right. So the name of test method. So before implementing test method, I'm just annotating it with at the rate test setup. So before implementing test method, I'm creating this test setup method. So if you remember while explaining test classes, I explained you the use of test setup method like in a test class. First you can create the data in this method and then you can query the data in the test methods. So here I'm going to create it. So I'm creating list of account where I will be adding account records because for this curable apex, like we need to pass account list and we need to pass idea of account record. So here first of all, what I'm doing in this list, I'm just adding a new account and I'm setting name of that account as parent. So this will be parent account that will be populated on individual account records. Now here I'm just iterating a loop integer i equals to zero i less than 100 and i plus plus. So this way 100 times this loop will be iterating then acclist.add and here I'm going to create new account records. So total 100 account records will be created. So test account then plus i. So this way total 100 account records will be created and they will be added into this account list. So one account will be added through this statement and rest 100 accounts will be added through this statement. Now after completion of this loop, I can just write insert acclist. So this everything is happening in the test setup method. Everything is happening in the test setup method. So this test setup method is basically creating the data which will be required to implement the test method, right? So now after this, what I need to do, I just need to implement the test method. So for that, I'm just writing at the rate is test. So here I need to implement the method. So private static void test cueable, right? So now here what I need to do, I will be querying the data which I created in above test setup method. So I'm creating ID variable named as parent ID and then I'm going to write SOQL. So select ID from account where name equals to parent. So we created one account record whose name is parent so that parent account I'm querying and it will be available at 0th index. So I'm just fetching its ID. So whenever we apply SOQL, so SOQL basically returns data in form of list. So through that list, like we already know one record will be queried because only one one account record is having name as parent. So that will be available on 0th index. From that 0th index, I'm just fetching ID field and that will be assigned into this parent ID. Okay, now after this, I need to query other 100 account records. So here I'm creating account list and applying SOQL select ID name from account where name is so I'm just writing it in new line account where name like test account. So here I'm using this like keyword which says the account records whose name starts with test account. All those account records will be queried. So here you can see all account records, all account records will be starting with test account name and then I will be added at the end. So here we are using name like test account. So all those account records will be queried, right? So this way, all the data is available. Now what I'm going to do I'm writing test dot start test and inside that I'm writing system dot nq job. Yeah. So before that, I just missed one statement. So before that we need to create instance of this class, cueable class. So naming it as update job new. And here in constructor, I'm passing account list and parent ID. Right. So we did it. We did it yesterday. If I show you the anonymous window. So we did it yesterday. So what we did, we queried the data and then we passed account list and parent ID here. So in test class, also, you just need to pass account list, which will be having all the account records. And this is the parent ID that will be populated on each account record. Now here you need to pass this update job. And then you can write test dot stop test, right? So this way, basically, I just implemented this code. So now one more thing is remaining. That is like to apply assert statement. So what will happen till here, nq job will be like creating asynchronous process, which will be starting execution of cueable epics class. And now after completion of this, that process, what we need to do, we just need to check or validate whether whatever operation we are performing through cueable epics that is actually happening or not. Right. So there's one question, Jana is asking, why did you add a single account record in line number eight? So if you remember, Jana, so here, we need to pass this account record ID, which will become the parent account that we need to populate here. Right. So that actually I have created here, one parent account record, that parent account record will be populated on all these 100 accounts. Right. So that is actually happening here. So I hope you understood this. Right. So now moving forward after this, what I need to do, I just need to apply assert statement. Right. So now I'm going to apply assert statement for you so that you can understand how exactly we can validate the result. So you can just apply system dot assert equals. So here I'm passing 100 and then I'm just squaring select count from account where parent ID equals to parent ID. So here we created one variable named as parent ID. Right. If this parent ID, this parent ID will be having one account record. If that account record ID is available on all these account records which we created under parent ID field. So on account record, we have a field called parent ID. If that parent ID field is equals to this parent ID, it means all those account records are having that parent ID populated and that counts should be equals to 100. Right. Because how many records we created here? How many records we created here? We created 100 account records. Right. So all those 100 account records will be updated through the queueable apex and here we are just validating it. So now I'm just saving this code. I'm just saving this code and now I'm just clicking on the run test so that I can show you how it will be executing. So it is throwing an error. So let's check what it is. So it is saying insert field so required field missing that is active. So while creating account record, we need to pass that field value as well. So here what I can do I can write active underscore underscore C equals to yes. And the same value I need to pass here as well. So I'm just saving it. Again, I'm clicking on one test. So again, it is failing. So let me just check why it is failing. So it is showing assert field and expected 100 and actually zero. So now it is something different. So you also try to identify mistake if I missed something. Okay, I think I just missed percent sign here. So let me just run it run it again. Yes, now it is running fine. So basically when we use like and you are using test account. So basically we need to use percent symbol because percent symbol shows like after test account there there may be some more values. It can be different for each account record. So if you use this percent sign so it will work fine and it will be query 100 record because we are updating 100 that one record like someone is saying 101 record. So basically that one record is the parent one so that we are not updating. It is actually populated on other 100. So now it is working fine. And if we go here, so we will be having the code coverage as well. Right. So 100% code is covered. So this way, this cueable apex test class I showed you from yesterday's example. Now moving forward, we need to understand scheduled apex which is also very easy. So first I will be explaining all the theoretical concepts related to it. And then we'll be seeing how we can schedule. So it is like similar to scheduled triggered flow. So you can run apex classes at a specified time. So basically if you want to run maintenance task on daily or weekly basis, so we implement scheduled apex and you need to implement schedulable interface in apex class. So this is the basic syntax. So whenever you are implementing any class and if you want to schedule that class, so that class must implement schedulable interface. Right. So if your class is implementing schedulable interface, then only it can be scheduled. And it will be having only one method that is execute. And in that method, you need to pass a parameter of type schedulable context. And then inside these parentheses, you will be implementing the code. So Jenna, which code you are asking test class. So I can share that code. Let me just paste it in the chat. I won't be able to because it is exceeding the character limit. So maybe in the telegram group, after the session, I will be posting the code. So Navin is asking, why did you return count? So basically count is to count number of records, those will be querying. So first parameter is 100. And second parameter is on SOQL. This SOQL will be basically counting how many records will be queried. So Jenna interface names you can search. So like you can find a picks documentation. So here, let's say we have a picks. So let me just search. So we don't have, yeah, here we have this one. So you can search for interfaces and yeah, so this is complete guide for a picks. And here I think you can search for interfaces where you can find details of interface, right? So we have lots of interfaces. So you just need to search them here and you will be able to see the documentation again, an example, right? So moving forward now, we need to understand scheduled apex like how we can implement and how we can test it. So quickly I'm going to implement one example in front of you. So creating new apex class and naming it as daily OPP processor. So daily opportunity processor is the name of the class. And this class is implementing schedulable interface. And here I'm going to define the method. So public void, I need to define execute method. And in that execute method, I just need to write schedule label context CTX, right? Now here I will be implementing the code that will be executing. So basically, I will be querying opportunity records. So here I'm writing as a girl like select ID lead source from opportunity where lead source is equals to null. And I'm applying limit as 200, right? So I'm just going to query 200 records where lead source is null. Okay, then for opportunity OPP, opportunity list. And here you can write OPP dot lead source equals to web. So this operation we performed with the help of batch apex as well. So this code we are directly writing in the schedulable apex and the scheduled apex will be running at particular interval, right? And later you can call a batch apex directly into the schedulable apex as well. So if you go this if you go and check this exercise sheet, so here I gave you one task for schedulable apex and one task for scheduler batch apex. So for that reference videos are available with the help of them, you can solve these and one example I'm demoing here. So we just need to query the data, then you can apply the business logic and then you can just apply update operation, right? And there may be chances like you receive nothing. So you can apply null check as well, right? So this way you can apply null check and here you can write the code. So this way our daily opportunity processor is implemented. Here we have implemented this schedulable interface. That's why you will be able to schedule it. So if I go here and search for apex classes, so I am searching for apex classes. Now under apex classes, I will be having a button. So here I have this button schedule apex. So I'm just clicking on this button. So here you will find this interface. So you need to provide the job name. So let's say daily opportunity processor. Then here you need to search for the apex class. So here it is. So we can see only one apex class because only this apex class is implementing schedulable apex, right? That's why we are able to see it. Other apex classes are not available here because those are not implementing an interface. So you can just select it. Now here we have frequency like weekly monthly. So weekly, so you can select particular day monthly. So this way you can select particular day of the month, right? And you can check multiple days as well. Then start date and date and preferred start time. So generally we schedule it for midnight, right? And start date and date accordingly you can select and days also you can select. So today is Thursday, start date and date. So this is selected. Now if I click on save, so it will be scheduled, right? Now how we will be checking whether it is scheduled or not. So you need to search for apex jobs. So click on it and here you will find. So you can see its status is queued. It is not executed yet, right? So submitted date time is available, job type is available status is available. Now here if we go, so we have this completion date. So whenever that time, date time will match, so it will start its execution. So this way you can schedule, right? So as simple, just implement the code and implement this schedulable interface, right? So if you have any questions, so just ask those questions. So Sachin is asking, is there any benefit using schedulable Apex? So Sachin, the main benefit is to run your Apex class as a particular date and time. That is the only benefit. If you don't want to schedule your Apex code, you can go with batch, queueable and future. So Jana, this will come from practice. The more business requirement you solve, the more you will be able to understand like which feature you will be using and how to start the coding. So this can be done through practice only, right? So here you can see I am giving you business requirements. So here you can see this is just a business requirement. So here I mentioned schedulable Apex, but in real-time scenarios, you just need to judge as well like which type of feature we need to use. So these type of business requirements you can use and you can just implement the code, right? If you have any more questions, so you can ask. I will try to help you out. So I'm just waiting for more questions than if there is no question. So I will be answering, sorry, I will be implementing the test class for this schedulable Apex. As soon as we can schedule tasks using flow in Salesforce. So if you go through the session tracker, so here, like when we discussed about flows, so here I showed you like how you can schedule flows. So here schedule triggered flow scenarios are available, right? So day number 30 if you go through, so you will be able to understand them. So such in for governor limit for Async Apex you can just search. So here let me search for governor limit. So we earlier discussed about it. So here you will be finding both the governor limits. So here you can see for synchronous and asynchronous governor limits are available, right? So this document you can search and I can share it in the chat as well so that you can have. Yeah, so we can we can call batch class and schedule Apex. So I already provided you one scenario for practice like how to schedule a batch Apex. So if you go through this recording, you will be understanding and try to implement this in your system. Yes, Divya, Kron expression is also there. Like right now I scheduled my Apex class through UI, but you can schedule your Apex class through Kron expression as well. So let me show you that as well. So we have Kron expression in Salesforce. So for Apex scheduler, you will be using Kron expressions. So these are something like that. So this way you will be scheduling. So you need to create an instance of your schedule Apex, then Kron expression, and then you can use system dot schedule. So this is the name, then schedule like Kron expression and then instance of your schedule class. Okay, so this is the document where Kron expression is implemented. And if you want to understand the Kron expression, so here we have this thing, right? So first parameter is second, second parameter is minute, third is our fourth is day of month, fifth is month, then day of week, then optional year. So this is totally explained here. Okay, so you can just try this as well. So I'm just sharing this link with you here and I'm just putting it here as well. So here I'm writing note, try to schedule with Kron expression. So here I'm just providing this reference document, right? So this reference document is available with you. So you can just try it and practice. Okay, so if you have more questions, you can ask. So Sunil, I already shared this, like how you can schedule through Kron. So just try it yourself. It is very easy. So in this document, everything is explained. So you just need to use this in anonymous window. So this code you need to write in anonymous window. So here you can write a class name like so our class name is daily opportunity processor. So here you will be writing daily opportunity processor. This you can understand with the table which is available in the bottom. And this will be same. Here you can just put any name, then SCH is the Kron expression variable and M is the instance that you are creating above. So if you write it in the anonymous window, so your code will be executed and your apex class will be scheduled. And for this also, whatever apex class you are using, that must be implementing schedulable interface, right? Yes, oh, yeah, you just wrote it correctly. So this is the syntax. Okay, guys, so I'm just moving forward and trying to implement new apex class for implementing test class for this one. So this is the last apex class that I'm implementing for today. So at the rate is test. And here at the rate is test. So basically, I just need to implement a method test method, where I will be implementing the data first. So we need to test schedule apex. So like you already know, we implemented it for opportunity. So we need to create opportunity data, right? So here I'm creating list of opportunity, then through loop I will be creating the data. So I will be creating two different type of opportunity records. So if I is less than 250, if I is less than 250, so 250 opportunity records will be created where lead source will be null. So here you can see we have lead source like if lead source is null, only those records will be queried. If lead sources having some value, so those records won't be queried. So here what I'm doing, I'm going to add data in this opportunity list. So we just need to create new opportunity. So I'm providing name as test plus I then stays name equals to prospecting, then close date equals to system dot today, right? So I'm just breaking this into two steps. So what is happening? We are creating new opportunity, then we need to pass the name. So here I need to use a parenthesis. So using one more. Yep, now it is correct. So new opportunity will be created. I'm just assigning name stays and closed it. Now here I'm just putting else and copying the statement as is and then pasting it. So here one more parameter I will be passing that is lead source, right? So this way data is created through this loop. Now after this what I need to do, I just need to insert this opportunity list. Okay, now I'm just writing test dot start test. And inside this what I will be doing, I will be like scheduling this particular schedule depicts. So here I also did one mistake. So I just need to use this. Okay, right? So moving ahead now here I just need to schedule. So basically for that I will be requiring a clone expression as well. So here I'm going to create it. So string SCH equals to and then here I'm writing zero space zero space zero space. And then I'm writing at risk. Okay, so here I'm writing string job ID equals to system dot schedule. Now here I need to pass the name. So process opportunities, then chron expression, and then new daily OPP processor. So here like directly I'm passing the new instance, right? So directly I'm passing the new instance like this. Also you can create it in different line and then you can use it. So that will also work, right? So this way what we did, we just created the data. Then after creation of data, what we did, we just created a chron expression. So actually it is giving some error. So I think I need to shift that chron expression inside. So line number 31 and 23. So what I'm going to do, I'm just cutting it from here and writing it here. Yep. So now that error is resolved and it is giving me more errors. Okay, no worries. I will see it later on. Yeah. So it is for semicolon. I need to use this semicolon. Now after calling this statement like scheduling it, so now what we need to do, we just need to query the updated records and we need to apply assert statement, right? So what assert will be doing assert will be just validating whether whatever changes like we have done in in the records. So those are done correctly or not. Right. So now I'm going to implement assert statement so that we can check it. So here I'm just creating list of opportunity, updated opportunities, new list of opportunity, right? So here, we need to query the data. So what we can do, we can write a SOQL select ID lead source from opportunity where lead source is web and here like we already having web. So here I'm just writing email from opportunity where lead source is web, right? So here I just modified lead source as email. So what will happen through this daily opportunity processor, 200 records will be queried and all those 200 records will be having lead source as web. So now here I'm just querying the records which are having lead source as web. So what I can do, I can just write system dot assert equals and I am just comparing it with 200. So the updated opportunity list, if its size is 200, then our test class will pass, otherwise it will fail. Okay, so now I'm just saving it. So it is giving some issues. Yeah, now it is saved. There is no issue. So I'm just removing unnecessary lines. So a test class is implemented now correctly. Now what I'm going to do clicking on run test, moving to tests tab and it is failing. So let me just check why it is failing. So insert failed. Okay, so opportunity amount we need to populate. So here what I need to do I just need to fill amount as well. So amount is required field. So at the time of opportunity creation, we need to populate that as well. Now I just saved this code, I'm just clicking on run test again. Let's see what happens now. So again, it is failing. So there may be some more issues. So we just need to open this and okay, so support for specifying both a day of week and a day of month parameter is not implemented. So actually, I just need to modify this. So in here, so in Chrome expression like here, I'm using this question mark. So if you want to understand this Chrome expression, so this table is the best way. If I take you here, so here you can understand. So in at fourth place, I'm just using this question mark, right? So like here you can use for three places, you can use 000 as well. For day of month, you can specify one to 31 or these values. Month, you can specify like this. Day of week, you can specify like this. An optional year, right? Description is available here like what does these spatial character means. So you just need to understand this properly. It will take you like some time, but it will be important if you understand. Otherwise, whenever you will be getting a chance to implement Chrome expression in any of the project. So with this document, you will be able to understand it. So I just modified it. Sorry. I'm just saving it again and trying to run the test class again. Yeah, so this time test class is working fine. You can see it is showing green tick. And if I come here and see, so it is having all the lines covered. Right. So this way you can just create. So in generally in test class, you can use this kind of a Chrome expression. Right. And this way, like you can just create the data, then you can call the schedule label apex, then you can just query the updated data and then you can apply asserts. So now if you have any questions, so you can just ask. Yeah, so summation admin project flow scenarios are there, but I have not implemented those. So I will be solving them while explaining development project. Yeah, so basically we need to understand the Chrome expression parameters properly. So I just provided random values for testing or demoing purpose in projects like you just need to understand it properly and then only you will be able to implement. So I hope with this way, like you are good with all the stuff related to apex programming. So we discussed apex programming, all the things, triggers, test classes, asynchronous apex. So now you just need to do lots and lots practice. Right. So home is asking one more question. Let me just take it. So back size of record is 250 and a third you provided to 200. Yeah. So some this is home. This is good question. But if you focus, so our schedule apex is basically processing 200 records on the limit is 200. So that's why I provided 200 and it is working. If I remove this limit limit 200, then here I need to write 250. So this is the catch here. Okay, so this way I hope you are good with the things what we have discussed and practice. So here you will find practice exercises. So go through that and how to schedule a batch. This is important. Practice it. Try to schedule using cron expression as well. So we have done 56 sessions so far. And from next week, visual force basics, I will be explaining and then we'll be having integration and then development project and then lightning component. So this will be the sequence for future sessions. Okay, so do more and more practice so that you can become expert. Thank you so much for watching these sessions and next session will be on Monday. So till then you can do practice at your own. So I will be providing Monday next week session links in the session tracker and I will notify you through different social media platforms as well. Thank you so much for joining this session. See you on Monday.