 Hello everyone. I'm Sanjay Gupta. I welcome you to Sanjay Gupta Tech School. StudySalesforce.com. This is my website. You can visit this. It is a video library to learn Salesforce. In this video, I will demonstrate you how you can implement Apex test classes for the Apex code where you have used S object. So as of now, whatever implementation I demonstrated, it was like basic implementation of Apex test class. But now you will get to know how to implement Apex test classes where S objects are included. So here you can see one Apex code is implemented. So name of classes, update accounts, method is update account. Here I have created a list of account whose name is account to be updated. Then I created a set. So set name is account ID set. Then I have a loop which is querying ID, account.id from contact where name like test and where account.id is not null. So all those records will be available in this con variable one by one. Then I'm checking whether con.account.id is available in the set. If it is available, then that if that account is available in the set, so this if condition will be false, so nothing will happen. Otherwise, what will happen? New account instance will be created. con.account.id will be assigned into ace.id. Then its active field will be equal to just rating will be hot and account will be added into this list and account ID will be added into set. So let's say we queried multiple contexts. Those are related to one account. So we don't want to do this process again and again for all the accounts. Those are common for several contexts. So that's why we are maintaining this set so that we update one account once only. So this is the idea behind this code. So this is basically a apex code. Now I need to implement its apex test class so that I can cover this code and I can test whether this code will be working fine. So I'm going to create apex class. Name of this class will be update accounts test. So I need to put at the rate is test. Then again, at the rate is test private static void test update account. Now here I need to create some data. So in previous videos, we we didn't use as objects. But here in this demonstration, we are using as objects. So accordingly, we need to create the data first and then we need to use that data to process this. So basically, this this query needs to find some context where name is equals to this test, right? And that context should be having account associated. So this kind of data first we need to create then only we can check its code coverage, right? So whatever data we will be creating in this test class that won't be inserting in your org, it is for test class only. So here I'm going to create account records. So account acc equals to new account. Now acc dot name equals to test account acc dot active equals to more and acc dot rating equals to. So here we are updating rating to odd. So initially I'm putting it as code, right? And then I can write insert acc. So this way you can insert particular account report. So remember again, this account won't be actually inserted in your org. This is available for this test class only, right? So whatever data we created apex test class, those are limited to test class only. They are not related anywhere with your org. So now I need to create a context. So the con dot first name equals to test con dot last name equals to test then con dot count id equals to acc dot id and then insert call. So this way I created both account and contact. Now it's time to call this method update account, which is available in this update accounts class. So this way I can call this method. So this is basically apex now it is not called through any trigger. So that's why we are calling this method explicitly, right? So if we call this method like this, so it should be static. So public void public static void. So I'm converting it into static so that we can call it directly. Otherwise, what we need to do we need to create instance of this class and then we need to call this, right? So I call this directly. That's why I converted that method into static. Now after execution of this method, this method will be querying the data which we created in the effects test class here. So it will find this contact and account associated. This account will be updated. So now we need to query this account here in this test class and then we need to apply the assertion. So if this account acc active and rating fields are updated to yes and what respectively it means your code is working fine. So what I'm going to do I'm creating new instance that is updated account here. I'm going to go and select ID active underscore underscore c rating from account where id equals to acc.id and now here I can write system dot assert equals. So here I need to put expected and then actual. So expected I'm putting a spot and actual I'm putting as updated account. So updated account dot rating. So this way I implemented this code, right? So if this account is updated so updated values will be available here and then I'm matching this. So I'm saving this code. I'm going to run test. So clicking here clicking on run test. So scrolling down. So at the bottom you can see this is executed and here you can see code coverage. So so a class is available here update accounts and it is covered 100%. Now if I go here and see this so it is covered 100% you can see all lines are covered right because we provided data according. Now by mistake if you put let's say warm here you are expecting warm and it is updating to hot. So in that case your test won't be passing. I'm showing you that as well. So I'm running test. So here you can see it failed. If I open these so here you will see the error. So expected warm and actual is hot. So this way it is failing. So sometime it happens like your expected is correct but your actual is incorrect. So in our case this is incorrect and actual is correct but I show you like if both are not matching. So your test will fail but actual code coverage will be maybe 100% because code is executed but your results are not matching. So that's why you need to implement assert equal. If you are not implementing assert equal so it means you are just covering your code. You are not validating it whether it is working fine. So this way whenever you are implementing a pics test class for the apex code where S objects are involved in that case first you need to create the data and then you need to call that method then you can query that updated data and then you can apply the assert equal. So again remember the account and contact which I inserted those are not available in the org anywhere. So these are limited to this test class and here I just created one account and one contact but in my introductory video related to a pics test class I told you like implement test classes for bulk testing. So I'm going to show you how we can do bulk testing in my another video that will be available after this. So you can watch that video like how we can create bulk data and we can implement test classes using that bulk data. So that is the best practice but this was small video that demonstrated you like if S objects are involved in your apex class then how to implement its relevant apex test class. So if you want to watch more Salesforce related videos so you can visit this website studyfieldfold.com a complete video library to learn Salesforce. Thank you for watching this video.