 Let's understand future methods in Apex. So this is the introduction of future Apex. So basically future Apex runs process in a separate thread at a later time when system resources become available. So basically a future method or future Apex is part of us in process processing. So it runs in separate thread whenever required resources are available. If you are implementing any future method in the future Apex, so you need to use this at the rate future annotation. In synchronous processing, all method calls are made from the same thread and no additional processing can occur until the process is complete, but it is not true with the asynchronous. So in case of future method, method runs asynchronously in its own thread, right? So this is the basic difference between synchronous and asynchronous processing. So this basically unblocks users from performing other operations. So it also provides higher governor and execution limits for processing. Now, when to use future methods? So if you want to make callouts to external WAP services, so in that case, you can use future method. So let's say you are trying to make callouts to external WAP service from trigger, so you cannot do that. In that case, you need to call a future method through that trigger and you need to set callouts equals to true. So this is the first use case of a future method, then you can process that needs to execute it in a separate or its own thread. So if you want to initiate a separate thread, like if your process is taking longer to execute, so you don't want to execute that synchronously. You want that to be executed asynchronously in a separate thread. So that you can complete your synchronous process in timely manner. So in that case also, you can use future method. Then if you want to isolate DML operations on different S object types to prevent a mixed DML error, so that also you can handle with the help of future methods. So this is basic syntax of future ethics. You need to define a class then add the right future before the method. This is the annotation. And here we can pass primitive types or collection of primitives. Non-primitives are not supported in future methods. And then you can query some records and you can apply the operations. So let's jump into developer console. And okay, before implementing one use case, I'm just creating a field on account. So basically I'm going to create one method which will be calculating total number of contexts available under any account. So I am creating new field of type number. So labeling it as number of contexts, clicking on next, visible to all the profiles, next and save. So field is created. Now I am doing a refresh. So now I need to create a class where I can implement the future method, right? So field is already available. So it's time to create a class. So I am labeling it as account calculator. So here, first time writing annotation at the future, then public static void count context. And here I will be receiving list of IDs and ACC IDs. Then I'm creating list of account, ACC list equals to, now we need to apply SOQL. So whatever IDs are available in this ACC IDs, basis on that we will be applying the SOQL. So select ID. And here I'm going to apply paid into child SOQL. So this is basically child relationship name. So along with account, all related contexts will be queried. So this is our SOQL, which will be having all the data related to account and context. So information will be available into ACC list. Now I need to apply for each loop on ACC list. So ACC list will be iterated and one by one each account information will be available into ACC. Now we need to apply ACC. Number of contacts equals to ACC.context.size. So here what I did through this ACC, I'm fetching this context, which is child relationship name. So it is having all the context associated to particular account. And I'm calculating its size and calculated size will be stored in this field that I created earlier. And after that I'm writing a date ACC list. Also you can apply and I'll check like if not ACC list is empty. So if list is not empty, then only date DML will be executed. Now after writing this code, you need to jump into anonymous window from where you can create instance of this class where future method is available. And in later stage, like if you want to initiate this from any other class, so that is also possible. So getting list of account first. So I will be fetching some account records so that I can pass their IDs. So select ID from account limit five, then list of ID, ACC IDs equals to new list of ID. Getting on this list of account list and ACC IDs dot add ACC dot ID. Now I'm going to call future method. So account calculator dot on context. And here I'm passing ACC IDs, right? So this way, I first queried the accounts, created a ID set, and I'm passing those IDs into this method and that method will be querying account along with related contacts. And contact size will be stored in this field which is available on account, right? So now it's time to execute this. So I'm clicking on execute, it is executed. Now we need to test it whether it is executed or not. So you can search for Apex jobs. So whenever you initiate any asynchronous process, so that will be available here. So you can see it is executed and Apex class name is available here, method is available, right? And this job is completed. Now you can go and test certain records. So what you can do here, you can apply system dot debug so that you can know the IDs and basis on that ID, like what account records are queried. You can go and check on the UI, like whether total number of contacts are populated on account or not, right? So this way I demonstrated you how you can implement future method and how you can call it. Now it's time to implement its test class so that we can ensure the core coverage as well. So I am creating one more class. So labeling it as account calculator test and that it is test annotation because it is required to make this class as test class. Then whatever method we define, we need to use at the rate test annotation there as well. So public static void count context test and here we need to create data first. So creating ACC list equals to new list of account. Creating on this list. So in these are equals to zero, I less than 250. So 250 records will be created. So here ACC list dot add new account name equals to test plus I. So account names will be different because I am using test plus I, so I is changing every time and whatever account instance will be created that will be added automatically to this list. And here I am writing insert ACC list, right? List of contact on list equals to new list of contact. List of ID, ACC IDs. So this list of ID will be storing or collecting all the IDs of account. Now I'm going to iterate on ACC list so that I can create related accounts. So on list dot add new contact. Here we need to fill last name. So let's fill first name as well. So first name equals to test or if you want to put account name like as first name or last name. So that is also possible. So first name will be test and last name I'm going to set as ACC dot name. And to link this contact with account we need to populate account ID field with ACC dot ID. So this way account record is created. It will be linked with the, sorry, contact is, contact instance is created and it will be linked with the account. And we are adding that newly created instance into con list as well. And here in this ACC IDs I'm going to add ACC dot ID. So this way everything is created. Now we need to insert con list, right? So con list is also inserted. So account is inserted, contact is inserted. Now it's time to call the method. So test dot start test. And here I'm going to write account calculator dot count context and we need to pass ACC IDs and then test dot stop test, right? So this way what will happen? This method will be executed and the future method will be executed and context will be counted and the counted value will be placed on the account. So now after this, we need to apply a session. So I'm going to query accounts. So select ID then number of context underscore underscore C from account. So we need to query this field because this field will be having some value after execution of that future method, right? So I'm query all the accounts which we inserted here, right? I guess there's some error, okay? I just forward to write this, yeah. Now I'm applying system dot assert equals and one comma ACC is zero dot number of context underscore underscore C. So all the accounts will be available in this ACC and here you can see we are inserting one contact under one account, right? So here I'm comparing one with this ACC zero. So zero index account will be having only one contact in this field like a counted value. So if both are matching, then this assert will pass otherwise it will fail. So if it fails, so we can throw some message as well. So context are not counted successfully. So this kind of error message will be displayed, right? So now I'm saving this test class. So at top right corner, you can see this run test button is available. So I'm opening this bar, moving to tests and clicking on run test. So test class is executing, so it is completed and here you can see nothing is breaking, everything is passing. So we implemented one method and that is passing, right? Now it's time to check the code coverage. So you need to go to this class and here you can see each line is covered properly. So 100% code coverage is there along with whatever assertion we implemented that is also passing. It means the code which we implemented it is working fine. So this is all about how to implement future method and how to write test class for the class where future method is implemented.