 Hello everyone, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to demonstrate a scenario based on Apex Trigger and Apex Test Class. Let's understand the requirement of Trigger Scenario. So requirement says when an account record is created, then create a related contact as well. So we need to create a trigger on account, then trigger handle class and the test class so that we can ensure core coverage. So I'm jumping to the org. So in my org, I already have account trigger. So for previous use cases or scenarios, I implemented the trigger. So account trigger is implemented here. Account trigger handler is also there and its test class is also there. So what I'm going to do, I'm going to reuse this because as per best practice, we need to create one trigger on one object, right? Now, the requirement says upon creation of account record, we need to create a related content. So in this case, upon account creation, contact object record we need to create. So even we need to take as after insert because once account record is saved to the database, then only we will be able to have the idea of that record. And if we have idea of that record, then only we will be able to link that contact record with that account, right? So we will be using after insert. Now, these two methods we need to run in case of before insert, right? So what you need to do, you can just apply if condition. So if trigger dot is insert, if trigger dot is before, then inside this block, you can put it. And in else, I can write trigger dot is after and here I can implement the method or maybe call the method, okay? Now I'm moving here and I'm going to implement one more method. So these two methods are already implemented. Now I'm going to implement one more method. So public static void create related contact and then list of account and ACC list, right? So this way I need to implement this method. So I'm going to copy and paste it from here. I'm copying the method name and I'm pasting it here, right? So if you want to watch like how these methods are implemented, so you can follow the previous video, those are available in the sequence. So here you can see this method will be called whenever your record is saved to the database in case of insertion. So this is the way you need to implement your triggers, like so that you can call before context with methods separately and after context method separately. And we are just inserting the record. So that is why this trigger dot new we can pass here as well. So it is common for both before and after context. So I can save this trigger. So trigger part is implemented correctly. Now here I need to implement the code. So I'm just going to iterate on this account list. So one by one each account record will be created and inside this loop I'm going to create contact instance. Now while creation of contact only last name field is required. How you can see that. So if you go here and try to create new contact and if you don't fill anything and click on save. So you can see only last name is required, right? But we will provide first name as well as last name. So you can write contact dot first name equals to test F name plus I then con dot last name equals to test L name plus I. So, okay, I won't be working. So what we can do, we can just add it. Let it be test F name and here we can add account name. So in last this test L name, account name will be added concatenating. Now after this, we need to add this contact record to the list. So before the loop, I need to create new list of contact. Okay. So now what I need to do, I need to add this con instance into this contact list. So con list dot add con, right? Why we are doing this? Because after completion of this loop, all the newly created contact reports will be added to this con list and together we will be able to insert this contact list, right? Now one more thing we need to do, whatever instance we are creating here, we need to link it with account. So what you need to do, you need to populate the account ID. So account ID is available in ACC ID. So through this, you can populate. And here I need to close this bracket as well. It is showing some error. So this way now your contact record is created. It will be linked with an account and it is added to this contact list. Now after this, sometimes if you need to apply null check for this con list before insertion, so you can do that as well. So you can write not con list is empty. So if it is not empty, then only you will be applying insert operation this way. So this way our business logic is implemented. Now if you try to create new account record, so a contact will be created automatically and it will be linked to that account also. So I'm going to create new account record and I'm clicking on sales. So in the related list, you can see, okay, three contacts are created. So I have other automations like flow and process builder maybe. So because of that, these two contacts are created, but through this trigger, this contact is created. So you can see here we provided first name as test F name. So it is there test F name. And last name is test L name and account name like test 301. So this way, like we can identify this contact is created through this trigger. And these two contacts are because of other automation. So if you have other automation, so you can deactivate them if you want to test it. If not, then it will be creating only one contact record. So this way code is working fine. Now next step is to create its test class. So here you can see account trigger handler test class is already implemented and two methods are already there. I need to create one more method. So here I'm writing at the latest test. So naming it as create related contact test. Now here I need to create sample data. So I'm just going to copy this part and putting it here. So here you can see what I did. I just created a list of account. Then I implemented a loop which will run five times. It will be creating five account instance. And here I'm just removing it. So what we need to do, we just need to insert account reports with account names. And after insertion, we just need to check whether a contact created or not. So here you can see I'm inserting the data. So this test dot start test and stop test is basically ensuring for this method, fresh set of government limits will be there. So we have three methods now and each method is having this start test and stop test so that separate set of government limits will be there for each method. Now I need to query the data. So what I can do, I need to query account reports, right? So list of, sorry, I need to query contact records, not account. So list of contact on list equals two. So here what I'm doing, I'm just querying contact record where that contacts account ID. This is a look-of-field. If it is equals to this ACC list, zero dot ID. So I'm just querying only one contact record. If it is present, it means contact record is created for particular account. That account is available in this ACC list, right? So what we can do, we can apply system dot assert equals and we need to check null. So we can have assert and here we can have a message like contact is not created, right? So if for any reason contact is not created, so this assert will be throwing this error message. And if this contact list is not null, then this assert will pass. So I'm just saving this code. So now here we have three methods. One is create related contest. Second is copy billing to shipping and third is update rating test. So all will be evaluated. So before that I'm expending this. We are here on tests, clicking on run test. So you can see all three methods are passed. There is no error. And at bottom right corner, you can see the code coverage as well. It is 100%. All 22 lines are covered for trigger handle and all six lines are covered for trigger. If you go here, so you can see the breakdown like trigger is covered through all the methods and completely as well. And from here you can see each method is covering like this method is covering 77%. This 95% this 92, 72% and overall 100% code is covered. Right. So blue lines, blue highlighted lines are showing like this code is covered. Okay. So this way I demonstrated you like how we can create a trigger and it's test class. So in this scenario I explained you the use of after insert because after insertion only this account ID will be available that we are using here to link this newly created contact with the account. Right. So this way I hope you understood the trigger scenario along with the test class.