 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to discuss about trigger scenario along with its test class. So let's first understand the requirement and then I will demonstrate you the implementation. So the requirement says when account active field is updated from years to know, then we need to update all related opportunities stays to close lost. If stays is not equals to close one. So if I take you to the org, so if I open any account record, so on account under details, you will see one pick list that is named or labeled as active. So if it is already populated as yes and you are changing it from yes to know, right? So old value will be yes and new value will be no. So in that case, once you make your account inactive like from yes to no. So if there is any related opportunity and on that related opportunity, if stays is other than closed one, so you just need to mark those stages as close lost. Right? So let's say here we have four opportunities and all the stages are like qualification, perception, analysis, value proposition and qualification. So all these stages should be converted into close lost automatically. Once we update the account active field from years to know. So this is the requirement that we need to implement. So we need to write triggered trigger on account because we will be updating the account record and after update will be the event because we are going to update the related records. And here I am going to implement or I am going to call a method. So account trigger handler dot update related opportunity stage and we just need to pass trigger dot new and trigger dot old man. Right? So trigger dot new will be storing the new state of the records and trigger dot old map will be storing the old state. So now I'm going to open account trigger handler. So at the bottom I'm going to implement the code. So I'm going to copy the first statement because it will be almost same. So I'm just going to copy the method name as well. So this way public static void then update related opportunity stage method is defined. It will be receiving trigger dot new in ACC list and trigger dot old map and old map. Now inside this map third. First of all, I'm going to create list of opportunity naming it as OPP list then new list of opportunity. Now I'm creating a set as well set of ID equals to ACC IDs new set of ID. Right? So this way a list is created and set is created. Now I'm going to implement a loop on ACC list. So inside this loop, I'm going to just check whether the account which is updated and which is available in this ACC list. So whether that account active field is updated or any other field is updated because whenever you update something on account, this trigger will work and trigger will be calling all these methods one by one. So we just need to validate like if that active field is updated from ACC list yes to no then only we just need to run this code. So here I'm applying if condition. So if ACC dot active is equals to no and now I'm just using old map dot get ACC dot ID dot active underscore underscore C double equals to yes. So what is happening here this ACC dot active underscore C this is the new state of that updated record if it is having no and old map is getting the account ID so we are passing ACC dot ID as key and this key will be fetching the associated value and from that value we are fetching the active field. So value is equals to like account record, whole account record. So from there we are just getting the active underscore underscore C field and if it is equals to yes it means old value was yes and new values no if both are matching in that case inside this if condition I'm just going to use ACC IDs dot add. So this is a set and here I'm just going to put the IDs. So if this condition is matching then only ID will go to this ACC ID otherwise not. So this way through this loop all the account records will be filtered and only those account records IDs will be available in this set on which we need to work further. So what you can do it may happen like there is no account where you updated this from yes to no. So before implementing the further logic what you can do you can just apply a null check as well. So you can write if not ACC IDs dot is empty right so this null check you can apply and inside this you can write the code right. So here I'm writing follow account ACC colon then select ID comma active underscore underscore C then here I'm writing inner query so that I can fetch related opportunity as well. So select ID comma stays named from opportunities. So this opportunities is child relationship name. So we are query account along with related opportunities. So that is why I'm writing it like this. So after this I just need to write from account where ID in ACC IDs. So those account records will be queried whose ID is in this ACC IDs and those account records will be querying the related opportunities as well. Now here I can apply if condition so I just need to check if ACC dot opportunities not equals to null. It means if particular account is having any related opportunity then only we need to proceed further. So this I am solving with the help of parent to child as well. So we require one more loop. So it will be rating on opportunities. So opportunity OPP colon then ACC dot opportunities right. So ACC will be providing all the opportunities one by one for each account record and those opportunities will be processed through this loop. Now inside this loop we just need to check the existing stage. So if OPP dot stays name not equals to closed one and OPP dot stays name not equals to closed loss right. So if this condition is matching then so I am just shifting it here. So if stage is not equals to closed one and stage is not equals to closed loss. So if opportunity stage is not matching with any of the value then we can just assign closed loss to that opportunity. So OPP dot stays equals to closed loss right. Now I can just add this opportunity into OPP list. So this way I just implemented the solution. Now we just need to check whether this is ending. So here it is ending. So here I can apply one more if condition if not OPP list dot is empty. If it is not empty then we can just update OPP list. So this way this trigger method is implemented. So I just saved it. I am saving trigger as well. Now I am jumping to this account and here you can see. So I am just selecting yes first and saving it. So earlier it was none and then I just selected yes. So there is no change in the opportunity stage. Now I am moving here and now I am going to change it from yes to no and I am saving it. So this time trigger will work and if I go to related list. So here you can see all the opportunities stays is set as closed loss automatically because this trigger fired right. So this way we tested it from the UI. Now we just need to implement the test class so that we can verify it from there through code as well. So on account we just need to implement a test class. So public static or we can write private as well. Private void then we need to just test this one. So I am just copying the method name and applying test. So here first of all what we need to do we just need to create some account and then we need to create some related opportunities and then I will be updating the account from yes to no and then we will just need to verify the opportunities whether the stays are updated or not. So I am going to take help from previous code. Okay so I am just copying this data and pasting it here. So what I did like I just created a CC list then through this loop account records are being created. So here we just need to set active as yes so that later on we can modify right. Now here we just inserted all the account records. Now after inserting account record we just need to create opportunity records as well. So I am just going to open opportunity test class. From there I will be copying this piece of code and pasting it here. So opportunity list is being created five time it will iterate and here what I can do I can just iterate on account list because for each account I want at least one related opportunity. So I am going to iterate on ACC list. So this ACC list which is right now like inserted. So I am iterating on that then opportunity is created name is this stays closed it amount is not required. So as of now I am just raising it. Now I just need to link this opportunity with particular account. So here I just need to write ACC.ID. So we are iterating on account list. So one by one account will be available into this ACC variable. So its ID will be assigned into account ID and then opportunity will be added into OPP list. Then I can just write insert OPP list. So this way with this code we just inserted accounts with this code we just inserted opportunities. So each account is now having one opportunity associated. So I am iterating on account once again and here on account active field I am changing to no. And after this loop I am just going to update ACC list. So all the account reports which we created here will be updated and active will set from yes to no. Now I just need to query updated opportunities. So I am just creating updated OPP list and going to select ID and stays name. So you can see like earlier stays name was prospecting. So when now I will be querying the opportunity so its stays should be closed lost. So from opportunity where ID equals to OPP list of 0.ID. So from this OPP list I am just querying first opportunity so that I can verify its current stage. So its previous stage was prospecting. Now I am going to apply system.assert equal and in this assert equal like I am expecting value should be equals to closed lost. And here we can see the actual value. So we just need to write this of 0.stays name. So this way we are going to compare. So there is a problem variable does not exist. Okay. So here is I so instead of this what I can do I am just concatenating account name. So this test class is implemented completely. Now I just need to run this test class. So I just clicked on run test. So here you can see all the tests are passing if I go here and just see the code covers for this method. Okay. So it is not covered. Why so? Because this condition is not matching. So here we are. Okay. Something is not done correctly. So we just created account and active as yes. We inserted account list and opportunities created. And then we updated account list from yes to no. So this should run. So I think the issue is this method is not executed yet. So I am just going to run this test again because this method is not available in these test methods which are executed. So I am just clicking on run test again. Okay. Got the issue. When I implemented this so I just forward to write his test here. That is why this method is not treated as test method. It is treated as simple method. So that is why it is not included. So I am just clicking on run test again. So this time you can see it is included because number of methods are 11. And if I expand so here you will see that method. Yeah. Last one update related opportunity stage test. Now if I go here and check the code coverage. So you can see all the lines are covered completely. Right. So this was my bad. Like you just need to write at the rate is test every time. So why it happened because I just copied. So at the time of copy paste you just need to be aware with these errors. Right. So I hope this way you understood like how we can implement this trigger scenario and how we can implement it's test class as well. Thank you.