 Hello, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to demonstrate a scenario related to trigger. Let's understand the requirement of this trigger scenario first. So when a case is created on any account, we need to put the latest case number on account in the latest case number field. So there should be a field called latest case number available on the account record. And when you create any case related to particular account, so the recently created case number should be populated under this latest case number field. So here we need to create a trigger on case because we are trying to create new case. And through that case, we will be populating the case number on this latest case number field. Then to support trigger, we need to implement trigger handler class and then a test class for code coverage. So let's jump to developer edition or developer console. So it is already opened. And now I'm going to create a case, sorry trigger on case. So I'm clicking on file new and then I'm selecting apex trigger. So if I click on this, okay, let me first check whether on case there is any trigger or not. So if you want to do it from the UI, so you can just search for case object. And here you can see the trigger. So right now we don't have any trigger. So I'm going to create new trigger naming it as case trigger and it will be on the case object. So I'm selecting case as an object. Now when a case will be created, so we just need to populate the case number on account. So we need to update the account in this triggers business logic. So instead of before insert, I will be using after insert even because I just need to update the account. So once case is saved then case number I will be using to update the account records. Now here I will be calling the method. So case trigger handler dot populate latest case number. And from here I will be passing trigger dot new. So whenever we create any case or any new records, so those new records will be available in this trigger dot new context variable. Now I need to create new apex class. So its name will be case trigger handler. Right. So this is this is part of best practice whenever you create any trigger. So don't write business logic in the trigger instead create a trigger handler class and inside this class you can write the business business logic. In particular method. So here I'm going to use this method public static void name of this method is populate latest case number. And here we need to receive list of case and case list. Right. Now here inside this method I'm going to write the business logic. So first of all, I'm creating a list of account named as ACC list. After creation of this list of account, I'm going to iterate a loop on case list. So all the newly created cases are available in this case list because from trigger we are passing trigger dot new and this trigger dot new stores all the newly created records. So one by one each record will be available with this CS. Now I just need to apply if condition to check whether CS dot account ID is not equals to null. Right. So if you create a case and if that case is not linked with any account. So in that case, there is no need to put a latest case number on the account because your case is not related to any account. So if CS dot account ID is not equals to null, it means that particular case is related to particular account. So if this condition is true, then what you can do, you can create a new instance of account. And in this instance ID, you can store the CS dot account ID. So it means this particular account we need to modify and I'm just going here under the object manager. And if I open the account fields and relationship, so I already created a field that is latest case number. So when you will be implementing this demonstration, so remember under account object, you need to create this latest case number field of type text. So I'm going to use it here. So latest case ACC dot latest case number equals to and now I need to populate CS dot case number. Right. So case number will be stored in this latest case number field. And now I can add this instance ACC to the list. Right. So this way one by one each case will be iterated and it will be checked whether it is linked to account or not. If it is linked to account then only this logic will be executed and whichever account we need to update those account records will be added into this ACC list. Now after completion of this for loop, we need to check null for this ACC list because it may possible like you created only one case and that is also not related to any account. So in that case this ACC list will be null. So you can check ACC list for is empty. If it is not empty, then you can write update ACC list. So remember you need to write update because accounts are already there. You are creating case but they are linked to accounts which are already there. So in that case you just need to update those existing account records. So this way this case trigger handler is implemented and this method populate latest case number is having all the business logic and this method we are calling here. Now after saving trigger and trigger handler, I'm just jumping to the UI and here I'm going to open any account record moving to related list and here you can see there is no case. If I go to details, so here latest case number is also blank. Now if I go to related and try to create new case. So here you can see some required fields are there status case or isn't so you can put some values and just click on save. So 1039 this is the case number which is created right now if I go to details. So here you can see latest case number is populated. Now if I go and create one more case and also notice like in my org these two record types are created if in your org record types are not available. So you can just ignore because these options won't be there automatically you will see this form. So I'm creating one more case. So it is 1040 and on account record you can see latest case number is updated automatically. So that is because of this trigger that we implemented here. So now after implementation of this trigger we need to implement its test class. So what we did we just created a trigger on case we created trigger handler class. Now we need to implement test class for port coverage. So now I'm going to implement that. So I'm creating new apex class and as per part of best practice your test class name should be like case trigger handler because it is your trigger handler class name and followed by test so that you can uniquely identify these classes. And whenever you implement any test class so it should be annotated as at the rate is test. Then if you define any method so that method should also be annotated with at the rate is test and then I'm just copying this method name. Right. So what we need to do here we need to create the data. So first I will be creating account records and then I will be creating the case record and then I will verify whether trigger executed successfully or not with the help of this test class. So I'm going to create list of account first and always remember whenever you implement your test class. So always create bulk data don't test your trigger by creating only one record in the test class. So here I'm going to create bulk records that is why I'm creating these lists. So I need to create list of account as well as list of case. Now I'm going to implement a loop. So I'm creating five account records and after that I can just add all these accounts into the list. Then I'm writing insert ACC list. So this account will be inserted. Now I'm going to iterate on this account list that we inserted right now. So five account records will be created. So the second loop will be treating five times and inside this loop I'm going to create the cases. Right. So so case CS equals to new case then we need to populate the values to the required field. So if I try to create new case. So here you can see status and case origin to pick list are available which are required. So this is status and case origin. Let's check the API name of these fields. So first time searching for status. So it is status. So I'm just copying it and pasting it and populating new. Then another one is case origin. So let's check the API. It is origin. I'm writing CS dot origin and equals to phone. Right. Now I need to populate account ID as well. So this I'm picking as ACC dot ID then adding this CS into case list. And now this loop is completed and after completion of this loop what I can do. I can write test dot start test. Inside this I can write insert case list. Then test dot stop test. So when I will be inserting case list so what will happen trigger will be initiated and it will be executing the business logic. So whenever you apply DML in your test class. So what you can do you can just cover that into a start desk start test and stop test so that whenever that insert DML will be performed. So a fresh set of governor limits will be there. So for example in your test class if you have two different methods. So for both the methods separate governor limits will be there if you use DML statement inside this start test and stop test. Right. So it is not mandatory but it is part of best practice. Now, when case will be inserted so we need to check on these account records whether latest case number is available or not. So what I can do. I'm going to query updated account records so select ID and I'm going to pick this API name then from account where ID equals to. So here we can use this ACC list so ACC list of zero dot ID and here I need to put colon as well. Right. So in this list five account records are available so I'm just picking the first one and this first one latest case number. I am just querying and then I'm going to apply a system dot assert equals right. So here what I'm going to do I'm going to write case list of zero dot case number comma updated ACC of zero dot latest case number. Right. So this way I'm trying to compare both so case list of zero case number. So first record in this case list case number should be available in the this updated accounts zero dot case list number. Right. So if it match if it matches it means your assert is passing. So before running this test class here under the test. We need to check so I'm just going to click on run test now. Okay. It is failing so let's check whether sorry why it is failing. So you just need to double click and here you can see if your test is failing. So here it is showing a session field expected null and actual is one zero one four. Okay. So what is happening here this case list of zero dot case number. Okay. So basically your this account is updated with the latest case number but this case list is not having that case number. So it means it is working fine. So what we can do we can just query these records as well because after insertion that case number will be generated. So this list is not having that case number. So what I can do. So this way the case which is available in this case list to first case case list zero dot ID that case I'm querying and that will be available here and I'm querying its case number as well. So here I'm writing new case list. Right. So now I'm going to run this test again. And here you can see it is passing. So it means both are matching this case number and account field latest case number. Right. So earlier why it was not matching because I use this case list. So whenever we insert any case any list so it will be having the newly created ID but field values you just need to query and then you can apply them. Right. So today I explained to you like how you can implement the test class for this requirement and they can be different ways to implement the test classes. So this is one of the way if you find out any other way so that is also okay but you just need to verify your trigger handler business logic is working fine or not. And I hope in this video you also understood like when test class fail so how we can check like what is failing. It is important like whenever your test class fail so how you can see what is failing and at which line number error is coming. Okay. So I hope this way you understood like how we can implement a trigger on case along with trigger handle class and the test class as well. Thank you.