 Hello everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to discuss a scenario based on trigger along with its test class. So requirement is when an opportunity stays is changed. So on opportunity, we have a field called stays. If it is changed from any value to any other value, then we need to create a task record on that opportunity. And after creation of the task, we need to assign it to maybe logged in user or the opportunity owner. So we just need to implement a trigger and trigger handler for this. And then I will show you how we can implement the test class so that we can cover the code. So let's jump to developer addition org. So I'm going to implement this code. So first of all, I'm going to open opportunity trigger. So opportunity trigger and its trigger handler are already implemented in my org because I just implemented them to demo you previous examples. So here I'm just going to add one event that is after update. So whenever opportunity will be updated. So we just need to create a new task. So here else if trigger dot is after. So you can see for insert a block is created already for update. There is a block which is checking is before is after as well. So we just need to call a method that will be available under this is after. So it will run in is update and is after context. And don't forget to use this event. If you don't write this event, then your code won't be worked. Right. So now I'm going to open trigger handler as well. So inside this trigger handler, you can see three methods are already implemented. So here I'm going to write one more method. So public static void create task and we are using it for updates. So we just need to receive two parameters. One is a new list and second is old map. So new list will be stored in this OPP list type of type is list of opportunity and old map will be stored in this map, which is of type ID comma opportunity. Right now inside curly basis. I just need to write the code. So first of all, I'm going to save it. And from here, I will be calling it. So I'm just copying and pasting the statement and the method name is create task. Right. So we just need to pass trigger dot new and trigger dot old map. So whenever you update any report, so it's new value like new state will be available in the trigger dot new. And its old state will be available in this trigger dot old map. And both are available here. So trigger dot new will go to OPP list and trigger dot old map will go to old map. So variable names can be like you can use any name as per the best practice and you can just write them. So now inside this method, I'm just going to create a list first. So list of task. So I just created a list variable task list of type task. Now I'm going to iterate on OPP list. So for opportunity OPP colon OPP list. Now inside this, I will be writing the code. So first of all, I just need to check whether on update of opportunity report, whether opportunity stays is updated or not. If stays is not updated, then we don't need to run this code. So new stays is available under OPP variable. So I'm just using that OPP dot stays name not equals to old map dot get OPP dot ID. And then stays name. So here what is happening? This old map is containing old values of that updated record. So it is a map. So that is why I'm using get. So get basically returns a value associated with a key. So key we are passing as OPP ID. Right. So the opportunity ID will be passed as a key and get will be fetching the related opportunity record. So from that, we are just fetching the stage and this OPP dot stays name will provide you the new value. So new values being compared with the old value if both are not matching. So here what you can do, you can create a new instance of task objects. So this way we are creating it. Now we just need to fill the required field. So if you go here, you will find this task tab. And now if you try to create new tasks, so you will see all the required fields that you need to populate. Right. So if I click on save, so here we can see we have subject, then we have status, then we have priority. Right. Then we have this name related to these fields. And then we have assigned to. So we just need to populate all these. So one by one, I'm going to demo you. So first of all, we just need to use what ID so task dot what ID equals to OPP dot ID. So what ID means like with which record this task will be related. So if I take you here and the object manager and if I search for task, so here you will find. So just go to fields and relationships. And here you will find this what ID here you can see related to here also we saw. So if I create new task, so related to means with which record it is actually related. Right. So this way what ID means related to then next is task dot subject. So I'm just writing demo. Then task dot priority equals to normal superiority is a pick list. So any pick list value you can pick then task dot status equals to not started. So it is also a pick list so you can pick particular value. Now, next is we need to assign owner ID. So owner ID we can assign like OPP dot owner ID. So this is one option or what you can do you can write user info dot get user ID. So this statement will give you the current or logged in user ID. So you can just place this selected information here. Right. So a task will be assigned to logged in user. So it depends on the requirement business requirement like what user you want to assign for this task. Right. So owner can be the opportunity owner like if you are owning the opportunity. So task will be owned by you or if you want to assign different user. So I showed you like how you can assign the idea of the logged in user. Right. If you want to assign any other user like other than owner or logged in user. So in that case you just need to query that user ID and then you can just pass it here. Now after this what I can do I can just add this task record into the task list. Right. So this for loop is implemented completely and now we just need to check it for null. So task list is empty. If it is not empty then what we can do we can just write insert task list. Right. So this way this code is implemented whenever you will be changing the stage of particular opportunity. So a related task will be created automatically. So trigger handler is implemented. Trigger is already implemented. Now I'm moving here and going to opportunity. Opening an opportunity and here under activity you can see there is no task. Now if I go to details and change the stage. Okay. Actually it is already closed one. So let me just pick any other. So I am opening this one. If I go to details so it is having prospecting as a stage and task is not created. So now I'm going to modify its value from prospecting to value proposition and I'm clicking on save. If I go to activity so you can see a demo task is created here. If I open it so you can see the information. So we just set assigned to as opportunity owner. Then subject is demo pride is normal status has not started related to the opportunity ID with with which it is related. So this way I hope you understood like through code how we can create tasks upon update of particular opportunity. Now after this we just need to write its test class. So I'm opening the test class which is already implemented for previous functionalities. So here also what we need to do we just need to create the opportunity and then we will be updating the opportunity. And then we need to query task whether related task is available or not. Right. So I'm going to take help from the previous code. So I'm just copying a little bit code. I just paste it. Okay. So we just need to test create tasks. So here I'm writing create task test. Right. So now we just need to create the data. So here you can see list of opportunity. OPP list is created. And this loop will be trading five times which will be setting all the required fees like name stays closed it and amount is optional. If you want to skip you can. And after that it is being inserted. Now after insert if you want to run that trigger. So what you need to do you just need to change the value of stage. So what I'm going to do I'm iterating a for loop on this opportunity list. So here I just need to write opportunity. So opportunity OPP and then colon then OPP list. So we are just iterating on OPP list or one by one opportunity will be available in this OPP variable. And now we just need to update the stage value and the position. So I'm just going to open it. Right. So this way I just updated each opportunity with particular stage value. And now I can write update OPP list. Right. So once this statement will execute so trigger will be working. And on update of this opportunity like the five opportunities will be updated. So each opportunity will be having a related task. Right. So now what we need to do we just need to query the related task and we just need to check whether task is created or not. So I'm just writing list of task task list equals to then select ID from task where. So we need to pick what ID where what ID is equals to OPP list of zero dot ID. OPP list of zero dot ID means the first opportunity, which is available under opportunity OPP list. So if that opportunities ID is available with any of the task report, it means new task is created. So here we just need to apply assert. So I'm writing system dot assert equals. And here we just need to write except expected or actual. So what we can do we just need to check null. So instead of assert equals let's apply assert. And here I can just write task list dot size. So if task list dot size is null like if if it is returning zero. So this assert will be breaking. Otherwise not. So here we can just add this let's say sizes equals to zero. So double equals to zero if task list dot size equals to zero. So in that case, it will fire or we can we can also check it for null equals to null. So let's keep it to zero as of now. So now I'm going to run the test. Okay, it is failing. So let's check which method is failing. So create task is failing. Assert failed. So let's write it in different ways. So I'm just writing it like not task list dot size. So task list dot size. If it is having size as one let's say so not. Okay, we just need true and false. Let's apply assert equals. So expected one actual should be one. So instead of assert, I'm just applying assert equals. So what I'm going to check like each opportunity will be having one task associated if we update the stays. So if task list dot size is equals to one. And if it is equals to one, then it will pass otherwise it will fail. So I'm going to run it again. So let me just save it first. So now I'm just clicking on run test and this time you can see it is passing. Right. And if I go here and check for the coverage, so you can see this method is completely covered. Right. So this way, you can just implement the test class for this requirement. So first we implemented approach. We created opportunity that we updated and then we checked whether related task is created or not. So I hope you understood like how with the help of after update, we can just implement this requirement on opportunity trigger. Thank you.