 In this lesson, I'm going to implement an example of batch epics. So in previous lesson, you understood what batch epics is, what is the syntax. So let's implement an example. So I'm going to create one epics class naming it as opportunity processor. So what I'm going to do, I will be querying all the opportunities. And in the lead source, I will be setting web. Right. So here I'm going to implement database dot vegetable interface. So this is a syntax for that. And here I'm creating one variable of type integer. So initially value of this variable is zero. Now I need to define start method. So public and I am using database dot query locator. Start database dot batchable context BC. So this is a first method that is implemented in this batch epics. So here I'm using database dot query editor as return type and start with the method name and this is the parameter. And here I'm going to use return keyword and database dot get query locator. And here I'm going to write SOQL. So select ID name from opportunity. So the data from opportunity object will be queried. And through this return statement, they will be returned automatically to execute method. Now after this. So maybe what you can do, you can store this queried information in a list and then you can pass that you can return that list as well. Now public void execute database dot batchable context BC and then list of opportunity and OPS. So this way we are defining second method that is execute and here two parameters are received. So the data or record collection that we are going to return from the start method that will be received automatically in this execute method in this list of opportunity. Now here I can apply an iteration. So opportunity OP colon opportunities. And here I'm going to write OP dot need source equals to that. Right. So what will happen like whenever you will be executing this match. So all the records will be queried and their lead source will be converted into that. So basically it depends on this SOQL like what type of records you want to query. If you want to apply any wear clothes or if you want to filter the number of records so you can do that as well. Now after this iteration I'm going to write a DML update OPPS and after that I'm going to write record count equals to record count plus OPPS dot size. So this way or second method is implemented. So this is start method then this is execute method now I'm going to define one more method that is finish. So public void finish. So here also I'm passing this parameter batchable context BC and method name is finished. Now here you can see I implemented this finish method and it is showing system debug and total records processed and this record count. Right. Now here one more thing I need to write database dot stateful. So actually I am using this variable and I want to maintain its state. So that's why I used this interface. So after execution like this record count will be a total number of records that is processed through this batch apex. So initially it is starting with zero but with every batch record count will be updated and with every batch it won't be like it won't start with zero. So after completion, we are displaying it here. So now after implementation, I'm going to save this. It is successfully saved now what I'm going to do. I'm going to open anonymous window and I'm going to execute this batch apex. So here I'm writing OPP processor space OPP process equals to new OPP. So I created instance now ID batch ID equals to database dot execute batch and here we need to pass this instance. Right. So the syntax I already explained in the previous lesson. So basically through this database class execute method this instance will be executed. The idea of that batch will be available in this variable. So now I'm going to execute the statement. So I'm clicking on execute highlighted. Okay. So I mistakenly clicked on execute highlighted. So I am clicking on execute. So it is executed. Now here, like, I need to refresh this page apex jobs where I can track. So here you can see batch apex is the job type. It is completed. Number of batch is fun. Failures are zero and the class which was responsible for this batch. It picks is mentioned here as well. So all this information is available and this is your apex job ID. Right. Now if I move to opportunity. So you will see under each opportunity like each record under opportunity each record will be having lead source as well. So I'm clicking on opportunity and moving to all opportunities. So here you can see total number of records are 56. That's why all records processed in a single batch because default limit is 200. Now I'm going to add some fields in this list view. So I'm going to add lead source. And I'm going to add modified by. So last modified date and clicking on save. So here you will see. Okay. So let me remove some fields as well so that we can view the information properly. Yep. So here you can see all the opportunity lead sources. And last modified date is also same. It is 22nd January and time is also same. So this way. I hope you understood how this is basically working. Right. Now jumping into developer console again. So here you can see various locks are generated. So if I open the locks. So I just need to check the debug because we used system dot debug in. Finish method. So you can open them one by one. And just click on this. So here you can see in the second log it is available. Total records processed 56. And we have total number of records as 56. So this way. Record count variable is working fine. So remember you need to use this database dot state field so that. This record count will maintain its. State. Now I'm going to show you one more thing like how to create multiple batches. So we have 56 records in opportunity. So now what I'm going to do I'm going to break it in. 1010 batches. So total six batches will be processed. So I am again clicking on execute. And here you will see. When I refresh it. So you can see total number of badges will be six and batch is completed. And total number of batches are processed. Six failures are zero. And earlier time was 1028 and now time is 1031. So if I refresh the space so you will see the difference. So again, it is executed for all the records. So this way if you want to break. The batch is in less number of records. So that is possible. And here we can see. Yeah, we have these. Logs so you can open them one by one and click on debug only. So here total count is intact 56. So this way, like if you if you want to reduce number of records in the batch process, so you can do. Now after implementation of this batch epics, I'm going to tell you how we can implement it's test class, because it is important to implement it's test class so that you can make sure this batch epics is covered. Properly, so that you can deploy it into production. So I'm going to create new epics class and naming it as opportunity processor test. Now, whenever we create any test class, so we need to use at the rate is test annotation before class and before method. So I'm doing that. Now private static void. That's that's class. Now I need to create some opportunity records. So I'm creating opportunity list. So I'm going to create 200 records. So this way, basically, I created instances of opportunity and I automatically added them through add method in the opportunity list. And after completion of this loop, I can write insert dml. So I'm writing insert OPP list. Now test dot start test. Opportunity processor OPP process equals to new opportunity processors. So I created instance of the class batch epics that we created and ID equals to batch ID equals to database dot execute batch. And here I need to pass this. I'm sergeants. I'm passing 200 as batch size. Test dot stop test. Now after this, we need to validate it using a session. So what do we need to do? We need to query opportunity records. So I'm carrying updated opportunities. So I need to write select ID from. Opportunity here. Lead source equals. And I'm writing system dot. Assert equal. And here I need to compare size of this updated opportunity. So I'm expecting 200. So copying this list passing it here. Dot size. So if updated opportunity list sizes equals to 200. It means our batch executed successfully. So I'm saving this test class. And once you save your test class, so you start seeing this run test button. So I'm clicking on this button. So now I need to move to this test tab. So here you can see it is executed properly. No failure. And now I'm moving here and trying to check the code covers and the 100% code is covered. So this way you can implement your test class so that you can make sure your batch apex is covered or not. Thank you.