 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 APIC trigger and test class. Let's understand the requirement of this trigger scenario. So when an opportunity is created on any account, we need to put the latest opportunity amount on the account in the latest opportunity amount field. So basically, first of all, we need to create this field latest opportunity amount on the account. And whenever on that account, we create any new opportunity. So that opportunities amount should be available in this latest opportunity amount. And then if you create one more opportunity later to that existing opportunity, so the newly created opportunities amount should be populated. And if it is null or blank, in that case, existing should be there. So to accomplish this, we need to create a trigger on opportunity. Then to implement the business logic, we need to create a trigger handler class. And then to cover the code, we need to write the test class for it. So I will be demonstrating trigger implementation, its business logic implementation and the test class implementation. So let's jump to developer console. And here I'm going to check whether any opportunity trigger is implemented in my org or not. So always remember if trigger is already there on particular object. So you can reuse that. Don't create new trigger for different, different scenarios for one object. If you already have any trigger implemented, so you just need to reuse that. This is part of best practice because if you implement different, different triggers and different trigger handlers, so in that case, order will not be guaranteed. So here I am going to open opportunity trigger. And here you can see one method is already called that is in before insert context, right? Now I'm going to write after insert event because when opportunity will be created. So we need to update the related account. So in that case, like we need to use after insert. So here I'm going to use these context variable like trigger dot is insert. If trigger dot is before, so I can just cut and paste it here. Then I need to write else part trigger dot is after. So now inside this block, I'm going to write my logic. So I need to call the function. So I'm just going to open opportunity to trigger handler. Right. So here one method is already implemented. I need to implement one more. So public static void, populate latest opportunity amount. And here we need to use list of opportunity and OPP list. So I'm just saving this copying this function name and I'm going to call it here. So opportunity trigger handler dot then populate latest opportunity amount. This is the function name and here I need to pass trigger dot new, right? So here you can see trigger is one, but it will be performing these two operations. So update description will run in the before insert context and the populate latest opportunity amount will run in after insert context. If you have more than one function, so you can just maintain their order of execution as well. So it is suggested to use always one trigger for one object. Now we need to fill the definition of this function. So here I'm passing trigger dot new. So as in every video I'm demonstrating like whenever you create new records, so all those records will be stored in trigger dot new automatically and this trigger dot new is a list. So if you create bulk records, so those bulk records will also be stored in this list, right? So this trigger we are implementing as a pulkify a process as a part of bulkify process. So it can handle more than one records if they are inserted and trigger dot new will be passing the list into this OPP list. Now here, first of all, I need to create account list because I need to update account records and those account records which I want to update. So they will be available in this list. So after creation of this list, I just need to iterate a loop on this opportunity list. So inside this loop, first of all, I'm going to check whether this OPP dot amount is not equals to null if it is not null and OPP dot account ID is also not equals to null. So what does it mean? Like amount is not null and opportunity is linked to particular account. If it is not linked to any account, then there is no need to update accounts latest opportunity amount because if your opportunity is not connected with any account, so in that case, there is no account to be updated, right? So if these two conditions are satisfied, then only this business logic that I'm going to write inside this if will be executed. So here I'm going to create an instance of account then ACC dot ID equals to OPP dot account ID. So through opportunity, like I already checked whether upon opportunity of count ID is null or not. So if it is not null, it means we know like which account is connected with that opportunity and that account ID we are passing in this ACC dot ID. So it means this account will be updated and what we need to update is so here we need a field. So let me just go here under object manager and I'm searching for opportunity and I think I already created our latest or recent. Okay. I need to search it on account. Sorry. Okay. It should be with name or not opportunity. So here that field is not available. So I'm just going to quickly create that. So it will be of type number. So I'm labeling it as latest opportunity amount and then I'm clicking on next visible to all the profiles next and save. So this way on account object that latest opportunity amount field is created. So I'm copying its API name. Now I'm just going to save this code because I need to refresh this developer console if I need to use that or maybe what I can do. I can just use the API and where I will be saving. So it will be reflected. So ACC dot latest opportunity amount equals to OPP dot amount. Right. And then inside ACC list. I just need to add this newly created instance. So this way what will happen one by one opportunities will be available in this OPP and this condition will be checking whether OPP amount is not null or account ID is not null if it is true. Then that opportunity amount will be copied into this ACC instance and we are adding this instance into ACC list. So after completion of this loop. We just need to update this account list. So what I need to do I just need to apply a null check because what what can happen like if you are creating any opportunity and whatever opportunities you are creating maybe they are not connected to any account or they their amount is null. So in both the cases this if condition won't be working and your account list will be null. So you can check it for null if it is not null then only you can write update ACC list right. So this way our business logic is implemented. Now whenever you will be creating any opportunity on account so that opportunities amount will be populated on that field. So I'm just going to open any account record. So here you can see two opportunities are available. If I go to details so we have. Okay, I just need to refresh because we need to search for latest opportunity amount field. Yeah, here it is. Now if I go to related list and try to create new opportunity putting a closed date then populating a stage and then amount as well. So I'm populating 10,000 as of now and clicking on save. Now if I go to details. So here you can see latest opportunity amount is populated as 10,000. Now if I go and try to create one more opportunity so that amount will be updated automatically. So I'm populating 20,000 this time and populating a closed date and then stays and then save. Now if I go to details. So here you will see 20,000 is populated as latest opportunity amount. So this way I hope you understood like how this logic is working. So after implementation of trigger and trigger handler class now it's time to implement it's test class so that we can cover the code. So what we can do I am just opening the existing test class which is already implemented. So again, if test class is already implemented. So try to implement one more method in that test class only so that all the trigger handler methods will be tested through one class. Right. So here I'm going to create one more method. So we need to test populate latest opportunity amount. So I'm using it here test populate latest opportunity amount. Now here what we need to do first of all we need to create some records. So I need to create account records and then opportunity records. So for both the purpose I will be requiring lists. So first time creating account list and then I'm going to create opportunity list right after creation of these lists I need to iterate a loop so that I can create some account records. So here for in teaser I equals to one I less than equals to five I plus plus and here I'm creating an instance of account a cc equals to new account and inside so after the bracket a cc dot name equals to test plus I and then I can add accounts into the list right now after completion of this loop I can just write insert a cc list. Now after this I need to iterate on this account list and then I need to create opportunity records. So all the newly created accounts are available in this a cc list. So I'm iterating on this a cc list one by one account records will be available in this a cc and now inside this loop I need to create opportunities. So if your test classes having any other method already implemented and in that you find some code that you can reuse so you can just copy that code and you can reuse it. There is another way like you can create test utility or data factory classes where you can have a common method and you can call those methods so that you can reuse that particular code and the number of lines will be reduced. So here opportunity name should be a cc dot name. This is the opportunity instance then name stays close date and amount are populated then opportunity is being added to OPP list. Now after this we can write test dot start test and inside that we can write insert OPP list and test dot stop test. So here you can see we have two methods. So here also I use this test dot start test and stop test and in the second method also I'm using this test dot start test and stop test. So basically when you will be running this test class so both the methods will be having separate governor limits. So that is why it is suggested to have these two statements whenever you write DML in your test methods. Now after this what you need to do we just need to query the account records because on account record we created latest opportunity amount field so it should be having this amount populated because we created five account records and for each account record we created one opportunity. So whenever opportunity is being created related to that account so that account should be having that opportunity amount populated on that latest opportunity amount field right. So we will be comparing that field with this amount if that field is having this amount it means your code is working fine but one more thing we need to add here opportunity account ID equals to ACC dot ID so that account and opportunity will be linked together. Now here I am writing list of account updated account and then select ID. Then this field from account where ID equals to ACC list of zero dot ID so I'm just squaring the first record that is available in this ACC list that we inserted so if that one record is having the updated value so it means the rest of the records are also having but randomly you can query any record and you can just apply system dot assert equal so here system dot assert equals than expected and actual so expected is available here 20,000 so maybe you can use it directly or you can use it with the help of this OPL list of zero dot amount and then updated account of zero dot latest opportunity right so this way you can write your system dot assert equal okay so what you can do you can just copy and paste it so here instead of writing 20,000 you can write OPP list of zero dot amount as well so in both the ways it should work fine right so I am going to run this test class now so here some tests are also available so now I'm going to click on run test and here you can see test is passing nothing is filling it means both the asserts are passing properly right and if I go here and if I show you like whether code is covered or not so you can just click here and select all tests so you can see all the lines are covered properly right so I just executed both the methods so do both the methods this whole opportunity trigger handler is covered anytime like if you want to run a particular method of this test class so what you can do just click on test then new run and from here you can select your test class it will give you names of available methods in that test class you can select particular method and just click on run so now you will see only one method will be executed so here you can see above two methods executed if I expand this so it will show you only one method right and if I go here and check so only one method executed test populated populate latest opportunity amount and with this like both are covering so it may happen like through one test method your whole trigger is covered so it may happen but if you have two methods so you should implement two different test methods in your test class so this way I hope you understood like how we can implement the solution for this trigger scenario so I just created trigger on opportunity then I implemented trigger handler so that we can have business logic and then I showed you how you can implement test class for code coverage