 Hello everyone. I'm Sanjay Gupta. In this module, I'm going to demonstrate future method example. Let's understand the scenario first. So we need to create a future method to count how many cases are created through phone, web, email. So these are basically pick list values. Those are available on case. So pick list value name is case origin. So we need to put the counted value on account in three different fields. So these three fields you need to create on account so that counted value will be available under those fields with the help of future method. And after implementation of future method, we need to ensure proper code coverage as well. So let's jump to developer console. And now I'm going to implement future method first, and then we'll be implementing its test class. So here, name of my class will be calculate case origin. And inside this, I'm going to create a future method. So whenever we implement any future method, we need to write this ad-read future annotation. So this will be the method and method name is count cases based on origin. And this method will be receiving a list of IDs and those IDs will be stored in this ACC IDs. Now I'm just saving this code and jumping to object manager. And I already created three fields. So if you want to run this code, so in your org, you need to create these three fields, cases through email, cases through phone and cases through web. So I will be using these three fields so that I can count cases and these fields will be populated those counted values. And these fields you need to create on account object. So now, first of all, I'm going to create list of account. So naming it as ACC list. Then I'm going to write SOQL select ID. And now I need to use these APIs. So this is basically parent to child SOQL. So along with account all related cases will be queried. This is child relationship name and ID and origin are the fields of case object. Now here I'm writing account where ID in ACC IDs. So this way our list will be created. So I'm just breaking it so that it will be readable. So these fields are of account and these fields are of cases. And we are ID in this ACC IDs list those accounts along with related cases will be queried. So I'm going to implement a for loop. This for loop will iterate on this ACC list. And inside this loop, I'm going to create three variables. So we need to count these three things, email phone and web. So I'm going to create three variables and initializing them with zero. So initial, web and phone. So initially, these three variables are having a zero. Then I'm implementing one more loop that is nested loop. So it will iterate on ACC dot cases. So first loop is a trading on ACC list. So one by one account will be available in this ACC variable. And through that ACC all the related cases will be treated in the nested loop. Now here you need to check if CA dot origin equals to. So here you can count one plus plus else if CA dot origin equals to web. So you will be counting web plus plus then else if CA dot origin equals to email. So we need to count email plus plus. So this way, cases will be counted and here this case loop will be terminated. Now after termination of this loop, all cases related to one account are counted along like basis on the origin and their values are available in this phone web and email field. So now what we can do, we can assign those values into these fields. So first time going to assign email, ACC dot cases through phone. Here I need to assign phones phone variable then ACC dot. This is through that equals to that. So this way all the values are assigned to particular case. So this way, whole this nested loop bunch will be treated and all the cases will be counted along with particular account. So after completion of these nested loops, I just need to check if ACC list dot is empty. So if it is not empty, so I have provided is not if this list is not empty, then update ACC list. If we pass collection of IDs, so this future method will run or synchronously and all the cases will be counted and counted value will be populated on the accounts. So now I'm saving this code and we need to test it. So before that I'm just moving to account and I'm just opening one account. So here we have two cases already available. So I'm going to create three more cases so that we can have at least one case for each origin. So here I'm putting origin as phone, then email and then back. Okay, so now I'm going to open developer. Sorry, execute an inverse window and here I'm going to create a list of account and I'm applying a query select ID from. So I'm fetching all the accounts, right. And now I'm going to iterate. So before that I'm creating list of ID ACC IDs equals to new list of ID. So we trading on ACC list and adding ACC ID into this another list. Now from here I can call this method. So just copying the name of method. And here I'm passing ACC IDs. Now before moving here, I'm just going to open a picks jobs. So here you can see. This scheduled apex is queued but other than that, no other job is available. So now I'm going to execute this code and you will see one entry there and then we will see this account, whether this code is working fine or not. So I'm just clicking on execute. And here doing a refresh. So here you can see future method is completed. And now I'm moving to this account. And here you can see two cases are created like their origin is phone. Two cases created through web and two cases, sorry, one case is created through email. So this way, if you have related cases so they will be counted automatically with the help of this future method. And it will run also increasingly because we are using this at the rate future annotation. So this is one part of this code. Now, another part is to implement test class so that we can make sure this code is having proper code coverage. So now I'm going to implement a test class. So name of test class will be. So if you are a developer so this implementation of test class is very much important. So if you want to implement a test class so you need to put this at the rate is test annotation before the class and at the rate is test annotation before the method as well. This will be the test method so test count cases. So here first we need to create the data. So what I'm going to do, I'm going to create a list of account. I'm going to iterate a loop. This loop will iterate five times. Now inside this loop I'm going to create five account records. So five account records will be created through this loop and I'm going to add those accounts into this list. So that through one DML I can insert these account records. So here I'm going to insert a CC list. So this way account records will be created. Now, after creation of account record, I need to create cases. So for that, I'm creating list of case and I need to create list of ID as well where I can store all account IDs those are created above. These two variables are created. Now I'm going to iterate account list. So I iterated this loop on a CC list. So first of all, I'm going to add a CC ID into this list. Right. And now inside this loop I'm going to create three loops so that I can create multiple related cases and always remember whenever you implement any test class. So you should test your apex code with bulk data. So this way like here I created five accounts. Now I'm going to create five, or maybe like multiple cases. Some cases, their origin will be phone for some cases or is it will be web for some cases or is it will be email. These are I equals to one. Then I less than three. So this this loop will create three cases. So case C equals to me case. Then C dot. It does equals to new. Horizon equals to phone. And I'm going to add this case into this list. Right. So this way through this loop. Three cases will be created and those cases will be having origin as phone. Now I'm iterating one more loop here again. This loop will iterate three times and this time case or isn't will be email. And I'm pasting it one more time. So again it will be treated three times and this time case or isn't will be. So this way total nine cases will be created for each account. So we created five accounts so one account will be having nine cases. So this way total 45 cases will be inserted with this code. Now, after this, I just need to insert this list. So basically, okay, there is some error. So let me just resolve that first. So that error is gone automatically once I save the code. Right. So this way basically I created the data and after completion of this loop. I'm going to insert. This list. So this way, at top we inserted accounts and now we inserted cases. So now it's the time to call that future method. So now I'm going to implement this block test dot start test and test dot stop test so that when this test class will run so new or fresh set of government limits will be available. Now inside this I'm going to call the method. So I'm just copying this method name dot and this method and here you can see we already added all the account IDs into this ACC IDs list and this is passing here and I'm putting a semicolon. So this way your future method will be executed. Now, after execution of the future method, we need to query the accounts. So I created this variable and here I'm going to apply one SQL. So we need to select. So I'm just copying this from here and putting over here. So we need to query ID and DC fields from account where ID in ACC IDs, right? So we are using this list. And if account IDs available in this list of ID, so those will be queried and will be available in this updated ACC list. Now I can apply a cert equal. So as we know, like for each account, we created three cases for each origin, right? So I'm just testing this three equals updated account zero. So this AC updated ACC will be having multiple account records. So I'm just checking zero index record if it's field this cases cases through email will be having three, then this assert will pass otherwise it will throw an error. So I'm just going to copy this. And here I need to change these field APIs. So I'm checking all three fields, whether they are updated properly or not. Okay, so here I need to add s as well assert equals. So this way this test class is completely implemented. Now I can just run it. So I'm just opening this tab and moving here, clicking on run test. Okay, so it is failing. So let's check what error is there. Okay. So actually, there is some mistake. I just created cases, but I forgot to add those cases to account. So I just need to add this as well. So this I need to add inside each loop. I saved the code. And again, I'm clicking on run test. Okay, so it is saying expected three in actual two. So let me see. Okay. So I got the point. Actually, I just used less than instead of less than equals two. So that's why it is counting like loop is retreating two times or two cases will be created so I just need to add this equals to as well. So these are minor mistakes that we do while developing code. So this way like I hope you understood how we can rectify the errors as well. So I again save the code clicking on run test. And this time you can see cases are passed. Right. So there is no matter it means all the assert are passing. And if I go here and click on this all test so you can see 100% code is covered. So this way, I hope you understood how we can implement future method for the use case that I showed you in the beginning. So I hope you understood how it is executing. Thank you.