 Hello everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to demonstrate a scenario with the help of trigger and test class. Let's understand the scenario. So based on opportunity stage upon opportunity creation or updation, we need to set the description field as opportunity is closed lost or opportunity is closed one or opportunity is open. So for example, if you're creating any opportunity, so on opportunity creation, you need to populate some value in the state field because it is required. So if it is closed one, then description should be having opportunities closed one. If it is closed lost, then description will be having opportunities closed lost as text. If stays field is other than closed one and closed lost, so description should be having opportunity is open. Then if you open any existing opportunity where stays is populated with some value and you change that stays field value. And according to the change, again, you need to update the description having one of these three values. So for this requirement, like it is working for both insert and update. So we need to implement a common method and that will be working for both the requirements. So I need to implement a trigger, then trigger handler, which will be having the business logic and the test class for code coverage. So let's jump to developer console. So in my org, I already have opportunity trigger implemented. So I'm just going to open that. So for one of the scenario I implemented that. So here you can see before insert and after insert events are available and these two methods update description and populate latest opportunity amount are available. Now, the requirement as per the requirement, this method will also be called in before insert. So what we need to do just copy paste and update description based on stage, right? And we need to call this method for update operation as well. So what I'm going to do, I'm just copying it, pasting it. And here I'm writing is update. Then inside is before we need to call this method update description based on stage. And in case of update, along with trigger dot new, we need to pass trigger dot old map as well. So trigger dot new will be having the new state of the record and the same record will also be available in old map and that will be having the old value of that field. So for example, if you are updating stage from prospecting to closed one. So prospecting you can find an old map and close one you can find in trigger dot new. And same method we are calling from before insert. So as a second parameter, we need to pass null here. Now I'm going to open its trigger handler where I'm going to implement the code. So public static void. Then I'm copying the method name. So here list of opportunity. OPP list and map of ID comma. Opportunity. Old map, right? So here in this method, I'm going to implement the logic. So what we need to do, we need to iterate a loop on this OPP list, right? So OPP list will be having all the opportunities that we will be inserting or updating. Now here we need to check whether we inserted the opportunity or we updated the opportunity. How we will identify that. So if we updated the opportunity in that case, this second parameter will be null. It means if old map is null, then opportunity was inserted. So we can directly check it like this. If old map is null, it means insert operation happened, right? Or if opportunity is null, then we need to perform some logic. If a old map is not null, then update happened and we need to check whether on update that stays field is updated or not. If stays field is updated, then only we need to execute the if condition or the otherwise not. So here I'm writing OPP dot stays name not equals to old map dot get OPP dot ID dot stays name. So this old map is a map. So if you want to get the value of map, so you need to use get method and you need to pass the key. The key is OPP ID, which we are receiving from here and its stays will be fetched. So the same record will be available in the OPP list through trigger dot new and it will be available in old map as well. So here new value of the stays will be available and here old value of the stays will be available. So if old map is null, then directly if block will be executed, assuming that record is inserted. If old map is not null, it means record is not inserted. It is updated and in case if it is updated. So we need to check whether stays is updated or any other field is updated. So if stays is updated, so this old and new value will be mismatched. In that case it will be true. Otherwise it will be false. And if it is true, then we need to write OPP dot stays double equals to closed one. If stays is closed one, so OPP dot description equals to opportunity is closed one. Else if OPP dot stays name double equals to closed lost, then we can write OPP dot description equals to opportunity is closed lost. Else if it is, if opportunity stays is not closed one, not closed lost. So it is other than closed one and closed lost. So we can write description equals to opportunity is open, right? So this way the complete code is implemented here. So this code will work for both insert and update operation that we need to check. So code is being saved. So I just saved trigger handler as well as I'm now saving the trigger part. If I jump here, now I'm going to create new opportunity. So here I'm populating stays as qualification. So it is not closed one, not closed lost. So here you will see the description will be opportunity is open. You see opportunity is open. So insert is working fine. Now if I change it to closed one, so I'm updating the stays. So update will work and here you can see opportunity is closed on. So this way both insert and update operations are working fine. Now we need to implement it's test class so that we can check whether through code. It is working fine or not. And like for deployment, we need to have proper code coverage as well. So for that I'm going to implement it's test class. So first we will be testing the insert part and then we will be testing the update part. And we need to annotate it with address test. Now here you can see some of the reports are already created in the other methods. So I'm going to copy this piece. Now let's understand this. So how we created the data. So first opportunity list is created then five times loop is iterating, which is creating bulk report. So always remember whenever you implement test class, so data should be created in bulk so that your code can be tested for bulk operation. So here opportunity name stays closed date amount. Everything is populated and then opportunities added into list. So five opportunities will be created and will be added into the list. Then after creation of those opportunities, we need to insert them so that they will be available in the database. Now after this operation one more thing we are using test dot start test stop test so that separate methods of test class will be having separate governor limits. Right. So this is the use of using test dot start test and test dot stop test. Now after that I'm just going to query updated opportunity list. So I'm selecting description from opportunity where ID equals to OPL list of zero dot ID. So here in this OPL list five opportunities are there. So I'm just wearing first one and if its description is updated. So it means others are also updated. So we can just check it with system dot assert equals. So expected will be opportunities open and action. So updated opportunity list of zero dot description. So this way this assert equals is implemented. If after the query description is updated and having opportunities open. So it means this code will work fine. Now we need to implement the update part. So I'm just copying it. I pasted the code so it will be for update operation. So at the time of insert opportunity stages prospecting then we inserted and now I'm implementing a loop. So here I'm writing OPP list and then OPP dot stage name equals to closed one and then update OPP list. So at the time of insert stage was prospecting then I updated it to closed one. So here this time we need to verify in assert equal like opportunities closed one. So if both are matching it means update is working fine. So I'm just saving this code. So there was typo. I'm just saving it again. Now I'm going to run the test. So it is failing. Let's check what is failing. Okay. So update description test. So this any other method is failing and yeah. Okay. So any other method is failing where assert is implemented. So basically what is happening now? We have two methods which are updating description. So we can just comment it out. So they may they may not be scenarios like both the methods will be updating the same pill together. Right. So if I comment it out and again run the test. Okay. Update description test. So basically what is happening on insert opportunity description is updated. So we what we can do like in this scenario like this piece is already implemented. We implemented a new scenario and we just want to run our two test methods. So what you can do just click here new run select your test class and select the particular method that you want to test. So I just want to test these two methods. So I'm just running them. So now you can see both the methods are passed. Right. So this way you can just test your implemented method. But anyhow, if you walk as a developer and existing code is also breaking. So you just need to do some work around so that that existing code should also work. So here what we need to do. We just need to modify this assert. So here we need to write opportunity is open. So in this case, it will be working fine because at the end description will be opportunity is open. So now if I run the whole test class, so it will pass. So here you can see all the methods are passing. So this time all four methods executed and all are passing. So this way I hope you understood like how I implemented the scenario in front of you. So implement this in your org so that you also can understand the implementation.