 Hello everyone. I'm Sanjay Gupta. Welcome to Sanjay Gupta Tech School. In this module, I'm going to demonstrate QABLE-APIX with an example and we'll see its code coverage as well. The scenario that we need to implement for QABLE-APIX. So the scenario is we need to create a QABLE-APIX class that inserts the same contact record for each account where industry equals to media. So we need to pass collection of accounts where industry equals to media and we will be passing one contact. We need to clone that contact and that same contact will be inserted under each account. So this we need to implement with the help of QABLE-APIX and along with the QABLE-APIX code, I will be demonstrating you how we can implement its test class. So first of all, I'm going to implement QABLE-APIX class. So I'm going to create new APEX class and naming it as contact clone. Whenever you want to implement QABLE-APIX, you need to implement one interface that is known as QABLE. So you also gone through future methods. So in future method, basically we can pass primitives and collection of primitives as an argument. But in QABLE, we can pass S objects as well. Right. So this is the basic difference between future method and QABLE. And in future method, you need to use Adelaide future annotation, whereas in QABLE, you will be using this QABLE interface. So now I'm going to create two variables. One is private contact con and another is private string industry value. And then public. I need to define the constructor. So I'm just copying this. The constructor will be receiving two parameters, contact con. And so let's make it con only. And here I'm writing this only. And inside this constructor, I'm writing this dot con equals to con. And this dot industry value equals to industry value. So wherever I'm using this keyword. So these are related to these private variables. And if I'm not using this keyword, so these are related to these parameters. Right. So this way this constructor is implemented. So here you can see there is one error is coming because we have not implemented execute method till now. So whenever you implement QABLE interface, so you need to define execute method as well. So after implementing this constructor, now I'm going to define execute method. And here I need to write QABLE context. So inside this method, I'm going to write the code so that this QABLE apex can clone the contact and those contact will be inserted to related accounts. So I'm just creating a list of account. And here I'm going to write one SQL. So select ID name from account where industry equals to and here I need to write that industry value field. So equals to this and I'm adding limit as well. So this limit is optional. If you want to avoid you can then I'm creating list of contact. On list equals to new list of contact. Now I'm going to iterate on the countless that I worried. So loop is iterating on this ACC list one by one account will be available into this ACC variable. Now, what I need to do, I just need to clone this con variable. I'm going to clone it. So I'm just going to use. Okay, so here I'm using con C equals to con dot loan and here I'm passing. Also is parameter. So this clone is a function that can clone an S object and it will be available into the C variable. Now I just need to set a count ID for this newly created a cloned contact. So I'm assigning ACC dot ID into this C dot account ID. Right. And now on list dot add and see. So this cloned contact will be having the account ID populated so it will be automatically linked to particular account and this contact will be added into this contact list. Now after completion of this, I'm just applying one if condition so I'm just checking whether this contact list is null or not. So I'm using is empty method if con list is not empty then insert on list. Right. So this way, this code is implemented. So I made a mistake instead of cueable contact. I need to write cueable context. So then it will be correct. So now if I save this, so you will see this error will be gone. Okay, so at line number 12. Okay, so it should be a count. Now code is correct. So I just saved it. Now I'm just opening anonymous window. And so here first I'm going to, okay, let's first create one contact one account with industry as media. So I'm having this count and here I'm going to populate industry as media. Okay, but it is not editable. Okay, let me create new account record. And here I'm populating industry as media. So it is created here you can see and related contact is not available. So from here basically, I need to pass one contact and one string value. What I'm going to do, I'm going to create one contact. I'm providing last name as vehicle ethics. Right. Now I need to create instance of that contact loan class. So the instance is created and here I need to pass this con and media. So basically if you see the code so in the constructor we need to receive this contact and we need to receive this industry value and basis on that. So it is passed and now ID job ID equals to some dot and Q job and here I'm passing this instance. Right. So before executing, I'm just refreshing the space. So here you can see there is no cable epic. So three rows are available. So now I'm going to execute and you will see fourth row will be available there. So I'm going to execute these. And if I come here and refresh. So here you can see cueable processes completed. And if I come here and refresh, so you will see your related contact and that contact name will be cueable ethics. So there will be two contacts named as cueable ethics. One I created from anonymous window and one is cloned and then created. Okay. So what happened? I just didn't write insert here. I just created this and passed. So it was cloned and inserted. That's why one contact is there. Right. If you write insert on, so it will be inserted in the org and then it will be cloned. So there will be two contacts. So then you will be implementing this code so you can apply this process. Right. So after explaining this now it's time to implement it's test class. So let's quickly implement the test class for this cueable epics. So public static. So let's create this method test contact long. So this will be the method and before that method I'm going to create at the rate test setup method. So at the rate test setup annotation is used to create setup method so that you can create the data and in this another method you can query the data. So I'm just going to create static void setup method and in this setup method I will be creating the data and that data we will query in this. So here I'm going to create list of account. So this way five times this loop will repeat and I'm going to create five contacts. Sorry, five accounts. And for each account, I'm populating industry as media. And in this list, I'm adding the account of the completion of this loop. I'm just inserting this ACC list. So all the accounts are inserted. Now I'm just going here. So this data is created here. Now here in this test clone class. So here I'm going to create one contact and that will I will be passing. So let's create that contact here so that we can query later on. So I'm just going to create one contact. Now here in this test method, I'm going to query the contact first. So contact count equals to collect ID from. And that we are last name equals to. And I'm just copying this name and putting it here. So this way the content which I inserted in this setup method, I'm able to query that in this test method. So what is the benefit of this setup method like you just need to create the data once and you can query it in any test method that you implement in this test class. Now after creation of this, sorry, after fetching this contact. Now I'm assigning industry value as media into this string variable. And then test dot start test. That's not stop test. Right. So these two statements basically we write so that we can have fresh common limits. Now, inside this, I need to call the method. So I'm just copying these two statements and I'm writing them here. So I just created instance of this contact loan class. And here I'm going to pass this CONT and instead of this, I'm just going to pass this industry value. So you can do both like you can store that string into a string variable or you can pass it directly as well. So here you can see this cueable instances and cute here so it will be executed now after its execution we just need to make sure rather related contacts are created or not. So I'm just applying assert equal so system dot assert equals so basically we created five accounts so five contacts should be there along with these accounts so I'm just having five here. Then I'm writing SOQL so select count from context so this query will run on contact and context will be counted here. Account ID in. So let's put it in separate line so count ID in select ID from account where industry equals to media. Right. So, this, this is the SQL select count from contact where account ID in and this is one more part of this SQL, which is querying the account and we're industries media. So those contacts will be counted, which are related to account and we're account industries media if it is equals to five, because we are like our cueable if it should insert five contacts because we have five accounts only we are industries media. So if it passes, it means your cueable apex code is implemented correctly. So I saved this code. Now I'm just going to run this cueable apex test class. Okay, so it is showing some error. So let's check what is the mistakes or insert field. Okay, it is requiring. So line number 24 is failing. So what you can do you just need to cut these statements from here and you need to paste them here and you can comment out this line because we need to create instance here so that we can pass if you created in the setup method so your test class won't work. And if you put it here so in this case if you run the test so it will pass. And if you go to your contact look clone cueable apex so it is covered 100%. So this way you can implement your test class to cover cueable apex for the required scenario.