 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is my website studiesalesforce.com or video library to learn Salesforce. In this video, I'm going to demonstrate how you can implement Apex test class while implementing trigger. So I already implemented a trigger and trigger handler. And first I will be explaining the concept and then I will write its Apex test class so that we can cover the handler code. So here you can see account trigger is created on account. It is having context variable like trigger dot is insert is before. So it will be calling this method copy billing to shipping and in update is before it is again calling copy billing to shipping. So these two methods are called on insert and update. Now here you can see this method is defined which is having this account list. If this ACC dot this Boolean variable. So this Boolean variable is created on account object. If it is true, then we need to copy all the billing information into shipping. So this is the concept that is implemented in this trigger. And if you test it from UI, it will work properly. But now we need to implement its test class so that through code we can have its code coverage and we can test whether functionality is working for both the cases like insert and update. So I'm going to create new Apex class. So its name will be account trigger handler test. So first of all, I need to mention at the rate is test annotation. Again, I'm writing at the rate is test then private static copy billing to shipping insert test. I will be implementing two separate method. First I'm going to insert account record, which will be having that Boolean variable as true. And in second scenario, I will be creating account which will be having that Boolean copy billing to shipping field as false and then I will be updating. So let's understand how we can test insert scenario. So first of all, I'm going to create account record. So in this video, I'm going to show you bulk implementation as well, like how we can run our test class for bulk data. So if we create single record, then also we can test our Apex code to Apex test class. But if we implement bulk, so that is a best practice. So first I'm going to implement one account and then I will be converting that into bulk. So ACC dot name equals to test ACC dot billing city equals to test city. ACC dot billing country equals to test country. ACC dot billing state equals to test state ACC dot billing street equals to test street. ACC dot billing postal code equals to this value. Then ACC dot. So we need to copy that field equals to prove. Right. So this way I populated all account values. Now what I need to do, I need to write insert ACC. So this account record will be inserted. And here you need to notice one thing, like I'm writing this test class for trigger. So in case of trigger, you don't need to call this method because upon insertion of account, trigger is calling this method actually. Right. So this is the basic difference. Like if you don't have any trigger and you're writing Apex class, so you need to call the method in the test class. But in case of trigger, you just need to insert update or delete the record and then methods will be called automatically through the trigger. So after this, I can create account instance. So updated account equals to select. So from these fields, I'm going to select only one field. Let's say I'm selecting shipping city from account where. So in where I need to put the ID. So I'm going to put ID equals to ACC dot ID. So what is happening? We assigned billing city, country, state, street, postal code, all billing information we provided, but I am fetching shipping information after insertion. So I just need to test, like after insertion, whether this code worked or not. This code, if it worked, in that case, shipping city value will be equals to billing city. So here I can apply system dot or cert equals. So expected will be ACC dot billing city and actual is updated account dot shipping city. If both are equal, it means a code is working fine and code coverage will be there. So I am selecting this test. So test class is saved. So I'm going to run this. So it is throwing an error. Test is not passed. So let's check the issue. Asheson field. So expected test city and actual is null. So I need to test this why this happened. Basically, this is not working because my trigger is inactive. So actually I implemented it for one of the demonstration that I deactivated it. So now I'm activating this. So this time it will work properly. So I'm going to run the test. So here you can see test is passing, moving here, checking. So everything is covered, moving here, checking all tests. So here you can see insert part is covered but update part is not covered because right now whatever code I implemented is for insert only. So first I'm going to convert this into bulk and then I will be creating one more method that will work on update so that trigger and apex class that is trigger handler both will be covered 100%. So now I'm going to implement a loop. So what I'm going to do, I'm going to create a list of accounts first implementing a loop. Right now I'm creating two records only. And later on, you can create as many as required. So here I'm putting this plus I so that new account name will be there and ACC list dot ACP. Now after that, I'm going to write insert. So this way bulk data will be inserted. Now one more feature is available that is test dot start test and test dot stop there. So what are these, if you want like whenever test class run so if you have separate methods so right now I have one method. So you'll have multiple methods and if you want like governor governor limits for individual method should start from zero. So in that case you can cover DML into this start test and stop. So for each method, governor limits will be the initialize. Right, so this is a best practice so whenever you use DML operation in these methods so try to use test dot start test and test dot stop. So you can use it once. Right, so this way I inserted. Now I need to write ACP list. Zero dot ID. So updated account will be available here. So here also. I need to write ACP list. So any record you can pick like I picked zero index record. If you want any other index record you want to come here so you can do that. So this means like if you are inserting bulk data. So on each record billing city should be assigned into billing information should be assigned to shipping information. Right, so that we are going to check now. So I'm going to run the test. So you can see test is passing. Right, if I run it for index one. So then also it should pass. So I'm running it again. So you will see third time. This green checkbox green tick will be available so it is available. If you check the coverage so it is 100% for class and for trigger it is 66% because before update part is not covered. So that I'm going to do now. Okay, so this way I hope you understood how we can create bulk data and how we can use test dot start test and test dot stop test how we can randomly test records to assert dot, sorry system dot assert equals. Now I'm going to copy this complete method and I'm going to paste it. So this will be for update. So here in this case, I am removing this statement. So first I'm going to create account record without checking that checkbox. So this these records will be inserted and now through this loop, I'm going to update the checkbox. So ACP list dot, so ACP list of I dot. So I need to copy that now equals to true. And then update ACP list. So this way I updated this complete list that we inserted with this value. I'm going to run test again. So you can see now both methods are passing. So earlier method was one now methods are two. So both are passing. If I see this trigger handler. So this code is completed. Sorry, this code is covered 100%. If I check my trigger so it is also covered 100% and here in this bar also you can see account trigger is completed. Sorry, account trigger is covered 100% and account trigger handler is covered 100%. So this way you can create bulk data in test class for both update and insert operation and this way you can implement your test class. So I hope with this demonstration you understood various things. So first, how to implement test class for trigger or maybe trigger handler. Second, how to create bulk data. Third, how to use test dot start test and test dot stop test. And fourth, how to implement different methods to cover insert and update operations. So I hope with this way you understood whatever I demonstrated in this video. If you want to watch more Salesforce related videos so you can visit studysalesforce.com or complete video library to learn Salesforce. Thank you for watching this video.