 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 Trigger along with its Test Class implementation. Let's understand the Trigger scenario. So on account, there should be two checkbox fields. One is New Contact and one is New Opportunity. Now, on account creation, if New Contact checkbox is checked, then a related context should be created automatically. And if New Opportunity checkbox is checked and Active equals to Yes, Active is a picklist available on account, then a related opportunity should be created. So either New Contact or New Opportunity is checked, then Contact or Opportunity should be created. If both are checked, then both Contact and Opportunity should be created. So this is the Trigger scenario that we need to implement along with its Test Class implementation. Now I'm jumping to Developer Console. So before implementing code, we just need to create two checkbox fields as well. So I'm jumping to Object Manager, clicking on Account, Fields and Relationships. So I'm creating two checkbox fields now. So first field label is New Contact, available or visible for all the profiles and will be available on all the layouts. Then clicking on Save, New Checkbox Data Type, Next, then New Opportunity, Next, Visible to all the profiles, Next and Save. So this way I created two checkbox fields on account object. Now in Developer Console, in my org, like I demonstrated some of the scenarios previously as well. So I'm just going to open my account trigger that is already implemented, but in your org, if it is not available, so you can create. So as Best Practice says, like for one object, we need to have one trigger. So here is my account trigger and it is working for before insert and after insert. Here you can see blocks are available. So once account is created, then we need to implement either contact or opportunity basis on that checkbox. So here I will be calling the method create contact or opportunity and I'm going to pass trigger dot new. Now I need to open account trigger handler as well. So in this handler, I will be implementing the business logic. So here I'm writing public static void and I'm going to copy the method name, then list of account, ACC list. So whenever we will be creating new account records, so all those account records will be available in this trigger dot new context variable. And we are calling this method create contact or opportunity in this trigger and this method we are defining here. So trigger dot new will be passing all the newly created account records in this acess list. Now here in this method, we need to implement the logic. So I'm just saving this trigger as well. Now I'm going to implement the logic here. So first of all, I'm going to create two lists. One will be list of contact on list, then list of opportunity. So this way two lists are created. Why I created these lists because there they can be multiple records, multiple account records inserted. So for each account record, we need to create contact basis on that checkbox. So there will be multiple contact or multiple opportunities created. So to minimize the number of DML, we will be adding all the newly created contact and opportunity in these lists. And then through this list, we will apply the DML operation. Now I'm going to iterate over ACC list. So this acess list will be containing all the newly created account records one by one. Those records will be available in this acess variable. So now I can apply condition here. So if ACC dot new contact. So this new contact is a Boolean checkbox field if I come here and check it. So we have new contact and this is the API name that I'm using here. Right. So this is a checkbox. So that is why I'm using it directly here. So if this checkbox field is true. So this true block like this if block will be executed. Right. And after that, I am using one more if block here. I'm using if ACC dot new opportunity and ACC dot active equals to yes. So these are the two blocks. So this will be for contact and this will be for opportunity. Right. Now here I'm creating one instance for contact. Then setting first name as test plus ACC dot name. Then con dot last name equals to test plus ACC dot name. So last name is required. So that we need to populate first name is optional. If you want to remove the statement, you can, but last name you need to populate. Then to link this newly created contact with account, you need to write con dot account ID equals to ACC dot ID. So this way, if for particular account, this checkbox new contact checkbox is true. So new contact will be created and we are assigning account ID into this lookup. And after this, what we can do, we can add this instance into the list. So this con instance, I'm adding into the list. So once this loop will be completed, so after completion of the loop, we can check whether this list is having some data. If yes, then we can apply the DML to create new contact records. Now after completion of this if block for contact, we need to implement another block that is for opportunity. So here I'm creating new instance for opportunity. Then on opportunity, we have three required fields. So first one is opportunity name. So I'm mapping it with account name. Then opportunity states. So here I'm writing prospecting one of the pick list value of stage and then close date. So in close date, I'm setting system dot today. So these are the three required fields. Those we need to fill while creating opportunity. Now I can easily add this newly created opportunity instance into the list. Before adding it into the list, I need to write one more statement that is OPP dot account ID equals to ACC dot ID. So again, this statement is to link the newly created opportunity with the account. So we are just populating account ID in the lookup field that is available on the opportunity. So this way this piece of code is also completed. Now here our loop is completing this for loop is completing here. So after this, I can apply null check for con list first. So if it is not empty, you can see I'm applying not. So if it is not empty, then I can write insert con list. If you want to apply this null check differently, so you can write it like this as well. If OPP list dot size is greater than zero. So this is another way to check whether some data is available in the list or not. So here you can write insert OPP list. So this way the code is implemented, right? So now it's time to test this code from the UI. So I'm just saving it and now I'm jumping to the UI doing a refresh. So now I'm going to create new account record. Okay. Let's check whether any other. Okay. So here we have created related contact and related opportunity function. So I'm just commenting these because these are implemented for previous demonstration. So I'm just commenting these so that because these functions will be creating one related contact and one related opportunity automatically. But we need to create contact and opportunity whenever that new contact or new opportunity checkbox is checked. So I'm just saving this now doing one more refresh. So I'm clicking on new and trying to create new contact. So sorry, new account account 1109 and here we will find two checkboxes. So here you can see new contact checkboxes available. So I'm checking this checkbox as of now clicking on save. So in the related list, you can see only contact is created. Contact is having first name and last name are same like a test, then account name, then test and account name, right? And opportunities are zero. So contact part is working fine. Now I'm going to create one more account. So ACC 2209 and this time I'm going to enable this checkbox new opportunity. And we need to set this active as yes so that opportunity will be created. So I'm clicking on save. So this time you can see only opportunities created contact is not. But if you create one more account and you check both the checkboxes new contact new opportunity and active is also selected as yes. So you will see both contact and opportunity will be created. So this way you can apply the condition basis on this requirement, right? So I hope you understood how I implemented this whole scenario with the help of trigger and trigger handler class. Now I am going to implement its test class so that the method which I implemented here, we can make sure it is covered properly, right? So I'm going to open account trigger handler test class, which I implemented already for previous demonstrations. Now here I'm going to implement one method. So you can see some methods are already implemented. So I'm going to implement one method here so that I can check the implemented create contact or opportunity method, whether it is covered or not. So here I'm writing private static void then test create contact or opportunity and here inside this method I am going to implement the logic and I need to annotate it with at the rate is test as well. Now, first of all, I need to create account. So what I'm going to do, I'm just copying this code from here and I'm pasting it here. So I'm going to explain it what I did. I just created a countless then iterating a loop five times, which will be creating the account records, right? Now here you can see industry set as energy, which is not required in this case. So I'm just modifying it as new contact and here I'm writing as true. Okay. So what I'm going to do, I'm going to run it 10 times and I'm applying if I modulus to double equals to zero, then this piece of code will run else ACC dot new opportunity equals to true and ACC dot active equals to yes. So five records will be having this new account as true and five records will be having this new opportunity as true and active equals to yes. Right. Okay. So this is not working like mod operator is giving some issue. So I'm just removing it. So basically we need to use math dot more and then I comma two. So we cannot use modulus operator directly in a pic so you can use this mod function which is available in this math class. And if this expression's result is zero, so this statement will be executed. If not, then this else part will be executed. Right. So this way we just created the data and account list will be inserted when account list will be inserted, then either context will be created or opportunities will be created. So now we need to check whether few contacts or few opportunities are created for these newly inserted account records. Okay. So here I'm going to apply one SOQL. So I'm going to fetch. Sorry. I'm going to fetch context. Now, select ID from contact where account ID equals to ACC list of zero dot ID. So here you can see like one by one. So we are using even odd method. So if I is even then contact will be true if I is odd then opportunity will be true. I will be odd. So first record will be for opportunity. So here I'm going to put one. So ACC list of one will be having a countercard that will be having related contact. Right. So now I'm fetching it in the contact list. And what I can do. I'm creating one more list of opportunity. And here I'm writing select ID from opportunity where account ID equals ACC list of zero dot ID. Right. So this way I implemented the logic. So what I need to do now I need to apply system dot assert. So, so I'm choosing this option. And here I'm writing con list not equals to null. And here I'm writing contact is not created. So if this con list is not null, like if it is having a particular record, so this assert will pass. Otherwise it will fail. And this error message will be displayed. And same. I can write for opportunity. So here I'm writing. Oh, be careless. Not equals to null. And here I can write opportunities not created. So I'm testing it for only one, one record, like the record, which is available on the zero index, the record, which is available on the one index. So you want to test it for other records as well. So you can just apply SOQL and you can query multiple records as well. So what you can do you can put all the IDs into a set and then you can apply in operator. So right now I'm going to save it and I'm going to run the test so that we can verify. So I clicked on run test. So you can see the complete test classes passing and if I expand this. You will see five methods are passing in which one is this create. Sorry. This test create contact or opportunity. Right. So this is passing and if I go here and click on all test. So here you can see this method is completely covered. Other methods are not covered because these two methods are not running because I just commented these two methods. So these two methods are not executing. That is why these two methods are not covered. But the code which we implemented in this demonstration is working fine and it is covering. So this way I hope you understood how we can implement the test class for this trigger scenario. So do implement this code in your org and test it. So this is all about the scenario that I demonstrated. So do implementation and if you have any questions so you can put them in the comment section. Thank you.