 Hello, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School in this video. I'm going to demonstrate a scenario based on trigger. Let's understand the trigger scenario first. So scenario says when an account record is created, then create a related opportunity as well. So upon creation of account record, there should be an opportunity record created automatically with the help of trigger. So we need to create trigger on account because we need to create account record from the UI. Then we need to create trigger handle class where we will be implementing the business logic. And then I will show you how we can implement the test class so that code can be covered. So jumping to developer console. Now I'm going to open account trigger. So for previous demonstrations, I already implemented account trigger. So I'm opening that here you can see three methods are already called. So I need to call one more method that will be responsible for creating opportunities. And here two events are mentioned before event and before insert and after insert. So when account is created and you need to create related opportunity. So in that case, you will be using account ID so that you can link that newly created opportunity with that account. So IDs on creation of account will be available in case of after insert only. So what I can do, I can just copy and paste it here and I'm modifying it to create related opportunity. Now I'm going to open its trigger handler. So here I'm going to implement the method. So I'm going to implement it here in the middle. So this way the block is created here. I'm just saving it and moving to trigger and I'm going to save it. So it is also saved and we are right now working on the insert part. So that is why I just trigger dot news pass because whenever you create any record or bulk record. So all those records will be stored in this trigger dot new. Now here I need to implement the logic. So first of all, I'm going to implement list of opportunity. So naming it as OPP list new list of opportunity. So there can be chances like you are creating bulk records bulk account records. So for each account record, you will be creating one opportunity. So here bulk opportunities will be created. So we just need to add those opportunities to a list and then list will be inserted in the database. So now I'm going to iterate on account list. So from trigger we are passing trigger dot new. So this trigger dot new is a list which will be storing all the newly created accounts and those account records will be passed into this ACC list here. So I'm iterating over this list. So as I hope, you know, like apex is case insensitive language. So we can write names and uppercase and lowercase that doesn't matter, but still try to follow as per best practice to follow the case sensitive nature. Right. So I'm just modifying these. Now I iterated on ACC list. So inside this, I just need to create opportunity instance. So this new opportunity instance is created. Now we need to fill the value. So first we need to set the opportunity name. So what I'm doing, like if you create any account, so you provide account name because it is required. So whatever account name is, I will be creating the opportunity with the same name. So I'm just passing account name here. Then we need to set the stage. So stage name by default I'm setting as prospecting. And third required field for opportunity creation is closed date. Right. So if you don't know, like how to check whether particular fields are required or not. So you can go and try to create new records. So I'm just trying to create new opportunity and clicking on save. So here you can see opportunity name, close date and stays. These three are required fields. So that is why I'm just populating values into them. For close date. I'm just populating system dot today. Now, after this, I just need to link the newly created opportunity with account as well. So I need to write OPP dot account ID equals to ACC dot ID. Right. So this ACC list is having all the account reports. And here we are calling this method in after insert context. So after insert context will be having IDs of the newly inserted account records and those IDs we can assign into this OPP dot account ID. Now here. In this opportunity list, we need to add newly created opportunity. So this way this loop will iterate on ACC list and for all the account reports, one opportunity will be created and that will be added into OPP list. Now, after this, what you can do. So for safe side, just apply a null check on OPP list. If it is not empty, then you can write insert OPP list. So your opportunity list will be inserted. And like whenever you will be creating any account record. So one opportunity will be there related to that account. Okay. Now if I go here and create any account record and click on save. So in the related list, you can see here one opportunity is created. Here one more opportunity is there. It is because of any other automation that is implemented in my own. But the trigger which we implemented here, we are using this name and we are using stays and dates. So all these information are populated. Now after this implementation, I'm going to show you how we can implement the test class for this method coverage. So I already have test class implemented. So this is account trigger handler test. It is for this trigger handler. So I need to cover this method now. So for that, I'm going to implement a method. These methods I implemented for previous demonstrations. So yeah. So here I'm going to write test method. So public static void create related OPP test. Okay. So whenever you implement any test class, so it should be annotated with at the rate test and the methods also should be annotated with at the rate test test. Now here we need to create the data. So here you can see I have already created data for testing context. So I'm just going to copy this so that I can reuse this. So here I created account list and I rated five times a loop and five accounts are created. Now after that, what I can do, I can just use this block of code. So this block of code is basically inserting account list and I'm using test dot start test and stop test so that we can ensure like fresh governor limits will be available for this method. So if your test class is having more than one test methods, so you can just enclose the DMS into these two statements so that you can have fresh governor limits for each method. Now after this, I'm just copying this piece and I'm pasting it here. So what I need to do, I need to convert it into opportunity. Then I'm modifying it to opportunity list, select ID from opportunity where account ID equals to ACC list of zero dot ID. So what I'm going to do, I'm just wearing opportunity record, which is related to a particular account. So if particular account is having one related opportunity so that will be available in this OPP list. Now here we need to use this OPP list. So if OPP list is not null, then this assert will pass. If it is null, then it will fail and it will display the message. Opportunity is not created. So this way you can implement your test method so that you can cover your trigger handler business logic. Now I'm going to run this test so that I can show you. So if you want to run the test, you just need to click on this test tab and then you can click on this run test button. So here you can see test is passed and total four methods are available here. So this is first method, then second method, this one. Then we have third method and we have fourth method. So all four methods are passed, right? Now if you want to see the code coverage so you can come to your trigger handler and you can click here and click on all tests. So here you can see all the lines are covered, right? Because we executed the complete test class so all the methods are covered because we have test methods for all the methods. So this way, if you want to implement your test method for the code that you implemented to create a related opportunity on account, so I hope you understood whatever I demonstrated in this video. So follow these videos so that you can learn different, different trigger scenarios and how to implement their test methods. Thank you.