 Hello. Hi, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So as you can see, today we are going to have day 48 of this Salesforce Learning Boot Camp. And in this session, I will be discussing about Apex Trigger. So this is week number 13. And this week, we'll be having three sessions on Apex and Apex Trigger. So this week, this topic will be completed. And in today's session, I will be covering how we can prevent triggers are in trigger, how we can use trigger dot is executing context variable, how you can use database class and add error method, right? So these are the topics which I am going to cover. Okay, so hi and welcome everyone. And this is about me. So if you have joined the session for the first time, so through this slide, you can know about me. So Madan is asking to create a telegram group. So Madan, for your information, that group is already there. So I can just share the link of the telegram group with you so that you can join and become part of that group. Okay, so hi. Hello. Namaste. And I'm on this slide with says seven virtual learning best practices. So I hope everybody's following these best practices. And I can see like in telegram group, people are discussing doubts with each other. And those who have question, they post the queries and those who are knowing the solution for those queries, they're answering. So I think the purpose of creating that platform is fulfilled. Now, you have a good platform where you can discuss your doubts and I'm doing live sessions. So I am trying to explain each and every concept in detail. And I hope you are following the exercise as well. Right. So keep following all these best practices if you want to become expert in Salesforce. So if I go further, so this slide, my favorite slide, it says consistency is the key. So in my life, I always follow things consistently. So you can see how consistently I'm doing this boot camp. I just completed three months last week and fourth month is started and this is week 13. So you also like have consistency in your life and do regular practice so that you can also become successful. And in today's session, I will be explaining the topic and doing demo and then we'll take your question and answers. So in this session, like I will be demoing a few examples. So I will try to have some questions after the demonstration in between. And this is the telegram group where lots of people are connected and they are discussing about their doubts. So if you have not connected, then you can just join and you can see the new development boot camp timeline. So this is for next six weeks, right? So 12 weeks we have already completed in the first six weeks. I completed all the admin related stuff. Then in next six weeks, I completed flow, apex and trigger. And in this, these six weeks, you can see, I will be completing apex trigger, apex test class, asynchronous apex, visual force, and two weeks are dedicated for integration. So if I take you to the session tracker, so here in the session tracker, you can see all the admin flow and apex related stuff is completed. And those who are following the recording so you can see all the recording links are available here in column F, right? So one by one, you can follow. And we are about to complete this part apex trigger. Then I will pick apex test class, then asynchronous apex. And here you can see I have added blank rows for visual force page and integration. So soon I will be filling all the topics here, which I'm going to explain you. So for visual force, I will be doing two, three sessions so that all the basic things I will be able to explain. And integration, I will be having total six sessions. So I'm in discussion with one consultant if he will be available. So he'll be delivering all the sessions. Otherwise, I will be delivering those sessions for you, right? And after that, like these I will redefine. So first I will shift this HTML CSS in JavaScript and then we'll see aura and then LWC, right? So I think next three months will also be required to complete this bootcamp. So three months we did all admin flow and apex related stuff. Next three months we'll be doing like apex classes, apex test classes, asynchronous apex, visual force integration aura and LWC, right? So this is about the next six weeks timeline. And those who are cybersecurity enthusiasts. So here you can see I added one more tab. So if you want to join cybersecurity bootcamp, so there will be one session day zero, where we'll be discussing about all the things related to cybersecurity. And if you don't want to become part of this, but you want your friend or junior senior, those who have passion about cybersecurity, if they want to join this bootcamp, so you can share the same session tracker. So you can see in the same session tracker, Salesforce admin and development stuff is available and cybersecurity is available, right? So people asking me, like is cybersecurity related to Salesforce? So yes, it is related to Salesforce as well. In Salesforce, there is a job role which is related to cybersecurity. So quickly I'm opening it and I'm going to show you. So if we go to career path on trailhead, so here you will find the cybersecurity careers. If you open it, so here you will find lots of trailheads related to cybersecurity, right? So we tried to pick few topics from these trailheads. Those are related. So you can just go through and don't think cybersecurity is separate. It is also part of Salesforce ecosystem, right? So in Salesforce ecosystem, there are lots of companies who hires engineers for cybersecurity purpose. And other than Salesforce also, you can become... You can have a job or you can become freelancer if you know about cybersecurity, right? So these are the topics we are going to cover. And in between the bootcamp, if there's need for some few more topics, so we'll be upgrading this session tracker for you, okay? So this is like a little bit updates about the week. So last week, I worked with my friend who will be taking all the sessions related to cybersecurity. And due to our collective effort, this session tracker is in front of you, okay? And Saida Reddy is asking, what about development projects? Yeah, so after these six weeks, maybe like before starting Aura and LWC, we'll do a development project as well. So the recruitment app, I will be having few features related to APEX, trigger, test classes, S and Cronus APEX. So together we'll be doing those as part of development project, right? So I will be adding that as well. So let me take a note. So here I will be adding a development project, okay? So I just added a check mark here so that I remember like, I just need to do a development project for you, right? So it will also be done after the integration sessions, right? So if you have not followed this platform which is doing everything free for the community and throughout this year, everything will be available free on this channel and that too live. And I'm going to create some recorded sessions as well. Those I was not able to complete. So I just want some spare time so I'm just searching about that. So soon I will be uploading some recorded videos as well. So if you have not followed, so just follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram and Telegram so that you can receive all the notifications and you don't miss any of the session. And all the important links are available in each live video's description, okay? So just share this channel with everybody in the community for the betterment of the society. Like I'm dedicated so you also share the information so that everybody can get benefit and they can save their heart and money, okay? So now moving on to the topic that we need to discuss in this session. So we need to discuss like how recursion is available in trigger and how we can prevent it. So before starting implementation of the code, let me discuss about the recursion. So basically recursion is a process where a method is called again and again. I am just repeating recursion is a process where a method is called again and again. So here what will happen? Sometimes there are scenarios where a process is called again and again and it doesn't break so that infinite calling process is known as recursion, right? So what you need to do if there is a recursive process available in Apix Trigger so you can prevent it. So there are certain measures if you apply those measures so your recursion will be prevented and your smooth execution will happen. So here I have two scenarios that I will be implementing so that you can understand how this recursion works in case of Apix Trigger, right? So I will be implementing this. Okay, so our first example is when any account is updated then update accounts description through Trigger to demo recursion, right? So first I will be implementing a code through that recursion will be happening, right? So through that code you will see a recursion problem and then I will show you how we can resolve that recursion problem, right? So right now I am focusing on the first scenario. So I will be implementing a Trigger on account update and I will be updating accounts description and you will see like there will be a recursion problem. So jumping here and I'm already logged into OR, right? So I'm on account Trigger where I will be implementing the code and in the account Trigger handler I will be defining the method, right? So what I'm going to do here in after update I'm going to call, sorry, I'm going to call a method. So method name will be update account and I will be passing two parameters Trigger.New and Trigger.OldMap, right? So in last week we already discussed about these two context variables. So Trigger.New will be storing the new copy of that record which you updated and Trigger.OldMap will be storing the copy of the same record but with old values, right? So now here in Trigger handler I'm going to implement this method. So I need to write public static void update account and here I need to receive that Trigger.New and Trigger.OldMap. So I'm just creating two parameters here. So Trigger.New will be stored in this ACC list and Trigger.OldMap will be stored in this ACC.OldMap, right? Now here I'm going to create one list of account naming it as account to be updated, right? So now here I'm going to implement a loop account ACC colon ACC list. So all the updated values, all the updated records are available in this ACC list. So I'm iterating on this. Now here what I'm going to do, I'm going to create one instance of account. Then in this A.ID I am assigning ACC.ID, right? So the record on which we are iterating that records ID we are assigning into this A. So this A is an instance which we are creating and we are assigning account ID into this A variable and this A description I'm going to update. So here I'm updating it with a value test recursion. Okay. And what I'm going to do, I'm going to add this A instance into this list, right? So what I did, I just added the newly created instance account A that we created at line number nine so that I added here in this list, right? Now what I need to do after completion of this loop, I just need to apply null check. So here I'm going to copy this list and applying is empty so that I can check whether it is empty or not. And then I'm writing update ACC to be updated, right? So this is the list. Okay. So this is a method and in this method, like this method will be called whenever you will be updating any account record from the UI. And what is happening in this method, you are again updating the same record, right? So what will happen? You will update your account record from the UI. Then this trigger will be initiating and this method will execute. This method is again updating the same record. So what will happen? This trigger will re-initiate. So this will happen again and again, right? So the same method will be called again and again because here on update, we are again writing update DML, right? So in case of update, if you write update DML, so it will create a recursive situation. So your code will run again and again. So now I'm going to show you the proof. So I'm just saving both account trigger handler and account trigger, right? Now I'm just moving to the UI clicking on accounts. Now here I'm going to open this account record and here you can see we have not checked any condition like if you update any field, if you update any field, your account description will be updated and you will see an error message. So what I'm going to do, I'm going to update the rating and I'm clicking on save. So here you can see this is an error message. So just read this error message carefully. It says account trigger execution of after update caused by system dot DML exception update failed. And why it is failed because maximum trigger depth exceeded. So it is calling after update again and again. You can see because we updated it from the UI, then in the trigger itself, we are updating the record. Then on that update it is again calling the trigger. So this is happening again and again, right? So this is basically a recursion situation. If you try to update your record from the UI and you update your record from the code as well. So that actually creates a recursive trigger, right? So what you need to do if you want to prevent the situation, so we have a solution. If you want to prevent the situation, we have a solution, right? So we need to identify if this method is executed once. If this method is executed once and again we are updating this record. So next time this method should not run. Next time this method should not run. So that we need to do. So what I'm going to do, I'm going to create new apex class and naming it as prevent recursion. And inside this class, I'm going to create a static variable public static Boolean. Naming it as first call and I'm initializing it as false. So first call is a variable here. I just created a variable named as first call, right? So what is what is happening? This variable is having false initially. Okay, now what I need to do? I need to go to the trigger and here I'm calling this update account. Okay, so before it, I am just writing if not prevent recursion dot first call. So this prevent recursion is a class and first call is a variable. Here you can see this variable we declared as static. So you all know like static thing we can access directly with the name of class. So similar to method we are calling this variable directly. So this is the name of the class and here is the variable first call. So initially first call is false and first time we want this if condition to be true because we are calling this inside if statement now. So initially if it is false, so here we are using not. So what will happen? Not will convert it into true. Not will convert it into true. So first time this method will be executed. But before execution of this method, what you need to do? You just need to set it true. So what does it mean? If you enter in this if statement first time. So your first call variable will become true and this update account method will be called. Now this update account method will be calling this update statement again. So what will happen? This trigger will be re initiated. But now first call is true. Now first call variable value is true. So this not will convert it into false. What will happen? Not will convert it into false. So second time this if condition will not work. So this is the solution to prevent the recursion. First of all, you need to create a class where you need to create a static Boolean variable initialized as false. And then this way you can just write your if statement and inside if if statement you need to call that method which is causing recursion. Right? So I am just saving this code. So it is being saved. And after saving what I will do? I will just save that record where we got the error. So this code is saved. Now moving here and I'm going to refresh the page. So again, I will be updating particular field and you will see there will no error. So if I update rating to hot suit will be saved. You can see and if I come here so you can see description is updated to test recursion. So this time with the help of that class prevent recursion class. We created this variable and here we applied this code. So this is the work around so that you can just prevent your recursion like recursion won't be implemented if you apply this process. Right? So somebody is asking can we declare Boolean variable in handler class also? So yes, we can. But why we declared it here? So if any other trigger also wants to use this variable. So it is a generic class and any trigger can call this class and use this variable. So that's why I created it separately so that in any other trigger also we can use it. Okay. So I hope this way you understood how we can implement the solution for trigger problem. Right? Now we have one more scenario. So that is available in front of you. So let me just move my picture a little bit above so that you can read the requirement properly. So just go through the requirement that we are going to implement and then I will be implementing the solution for you. So we have one more requirement that says when an opportunity is updated to closed one or closed lost. Then update description with opportunities closed one or opportunities closed lost as per the stage. Right? So here what you need to do, you will be updating your opportunity from the UI. You will be updating your opportunity from UI and you will be updating the stage field. And once stage field is updated in the trigger, you will be updating the description. So what will happen once your opportunity will be updated from the UI and again your opportunity will be updated from the trigger. So again it will cause a recursion and that recursion problem you can resolve with the help of that static Boolean variable. Yeah. So let me just pick few questions. Those are available. So let me show you here. So Sachin is asking why we were not getting recursion issue. So in earlier situation we never encountered any scenario where we were updating record from UI as well as we were updating the record from trigger method or trigger handler method as well. What we did we updated from UI or we inserted in the trigger. So if you update from UI as well as update from trigger as well. Right? So in that situation recursion will happen. Yeah. So this is the only situation where you will encounter the recursion. Yes. We don't need any method for prevent recursion class because that is just having a variable. So we are not implementing any business logic. So that's why just variable is required. Yes. We can stop. So this prevent recursion is similar to utl class. If you want to create it with utl name. So that is also possible. Yes. We can set that acclist directly as well to update. But in this example I showed you like if you want to create new instance. So that is also possible. And in case of after trigger. So one more thing that you need to remember we are calling this method in after trigger. So in case of after trigger this acclist it is receiving trigger dot new. So in case of after in after update that trigger dot new is read only you won't be able to update. So if you up if you pass this directly here like if you use that for updates so it will also throw an error. So you won't be able to use it. So that's why this instance is created and that is being added to the new list. Right. So in case of after update that trigger dot new is basically read only so you won't be able to update this account list. Right. So Jenna I already told you like if you are updating any record and through trigger also if you are updating the record in that case like it happens in insert and delete generally don't it doesn't happen because in update you do it two times in other cases you won't be able to do. So this is the only case where this recursion will be available. Yes Manan we can apply that logic but that logic we apply in the trigger handler not in the trigger because in trigger we don't implement a loop loop we implement in the trigger handler. So inside the loop we generally implement that. Okay. So now I'm going to implement solution for this next problem. So it says I'm again reading the question. So whenever opportunity is updated to closed one or closed loss then we need to update the description. So here the trigger you will be implementing on opportunity. So here I'm going to check whether we have any trigger on opportunity or not. No we don't have any trigger on opportunity. So I'm going to create a trigger for you. So first of all clicking on file new apex trigger. So here opportunity trigger. So creating trigger on opportunity and here I'm writing after update if trigger dot is update if trigger dot is after then we need to call a method. So opportunity trigger handler update description from here we will be passing trigger dot new and trigger dot old map. Right. Now I'm going to create one more class and this class name will be opportunity trigger handler. Right. So just understand this requirement carefully because it is important. Right. So here I just implemented this. So I think it is very easy for you now because you have implemented lots of triggers. So here I'm going to implement the method. So public static void. So method name is update description and here we need to create list of opportunity and map of ID comma opportunity old map. So this way this method will be defined. Right. And here also I'm saving this code. So now what I'm going to do I'm going to implement this method here so that you can understand the logic. Right. So first of all I'm going to create a list of opportunity. So in this opportunity list all the updated opportunities will be added. So now I'm going to implement a for loop. So for opportunity OPP then here I need to write OPP list. So we are iterating on opportunity list one by one opportunity will be available into this OPP variable. Right. So here I am I'm going to check if OPP dot stage name not equals to old map dot get OPP dot ID dot stage name. Right. And okay. So if it is changed like if your stage is updated then again we need to write if OPP dot stage name double equals to closed one. Right. If it is closed one. So what you need to do so before it I'm going to create one instance of opportunity. So opportunity O equals to new opportunity. Right. Now here if this line number 11 condition is true and line number 12 condition is also true. So what I'm going to do I'm writing O okay here we need to store the ID as well. So O dot ID equals to OPP dot ID and O dot description equals to opportunity is closed one. Right. Then here I'm writing else if and copying this pasting it here if it is closed lost. So again I will be using these two statements pasting them here. Okay. So now I think you can relate what I did I just checked whether stage is updated or not if stage is updated then I am setting the ID and then I'm setting the description then I'm checking whether it is closed lost again I'm adding ID or description because either this if condition will run or this else if condition will run. Okay. So this way the code is implemented now after completion of this if else like this this if block after completion of this if block what you can do you can just pick this list then add opportunity here right opportunity will be added then after completion of this loop you can apply if condition and then check whether it is empty or not if it is not empty you can update it. So this way whole code is implemented in front of you right. So first of all I created the list then I iterated on the loop like I iterated on the OPP list and then inside this I created one instance then checked whether stages updated or not if yes then I'm checking stages closed one right if it is closed one then description will be updated if it is updated to closed lost then description will be updated. So this way like after if we are adding that opportunity into the list and then we are updating it. So what we need to do first I need to update from the UI and then this code will run and this code will again update that opportunity so this will happen again and again right. So now I'm moving to opportunity and I will show you by updating opportunity right so here I'm going to open this opportunity if I go to details if I go to details so here you can see description is having opportunities open description is having opportunities open and opportunity stages prospecting now I'm going to convert it into closed one and clicking on save so you can see here we have an error so it is saying ID is not specified line number 20. Let me check it is showing some different error. Let me just shift it above I'm going to save it again. Yep this time we are receiving that records an error. So what you need to do you just need to store this outside there's these fls block right so that it become available inside that loop because if we set it here this ID we are setting here so it is becoming local to these blocks right so you can just set ID outside the if just after the initialization and here you can see these errors are coming right now what we need to do we can just reuse this prevent records on class so the code which I implemented here I'm just copying it and I'm going to call it here right so this way first time this method will be called and second time it won't be called because first time this first call will be false and through this not it will become true so when it is false so it this if condition will become true then next time it is set as true and this update description will be executed then next time when trigger will be called so this first call is true now and not will convert it into false so next time this update description method won't be working so the same prevent records on class first call variable we are reusing here that's why I created it separately. Okay and if I go here and click on save so it will be saved now and you can see stages closed one and description is updated to opportunities closed one so this way with the help of this class prevent records and I just showed you how we can prevent the recursion right so I know this is a complex process and you just need to go through the recording once again so that you can understand what I did and implement both the scenarios discuss it in the telegram group and still if you will be having any problem so I will be jumping in the telegram group and we'll be trying to resolve your doubts and if you have any doubt you can just post that question in the chat so that I can take it right now so either it is asking please explain one example of trigger implement without class handler so I just explain trigger implementation without class handler in the beginning in the first trigger session I just did that so you can just go through and watch that recording and as per best practice every time you need to implement trigger with the class handler so it was asking can we use before trigger because it is on the same object so it wasn't before trigger we don't use dml statement so that's why I'm you I'm using after update for demoing purpose so money is asking why you created a OPP instance yeah so opportunity instance I created because I already told you in after update this trigger dot new that we are passing in this OPP list so this OPP list is read only so we need to create one more list and in that list if you want to add any instance so you just need to create new instance and you can store the ID and then you can store the value and then you can add it so this is the process because if you add anything from this OPP list so that is read only so that's why we created the new instance here so I think Saroj I already explained this why we didn't use OPP directly because it is read only in case of after update so Naveen Boolean is true for preventing off recursion yes so if you see here in the trigger so first time this first call is false initially it is false and first time it will run and when it starts its execution so we are setting it true for next time so next time whenever it is true so not will be converting it converting it into false and this method won't be executing this way it is working right so you actually need to implement all the examples and then only you will be able to understand this so Janna like recursion means when a process is calling again and again so that is basically recursion right so Praveen you asked some question but your question is not clearly asked so if you can ask again so when I if you update anything else it won't be showing you recursion because in that case if you update anything else so in that case yeah in that case also it will be working because here we are adding that instance yeah so it will it will break so what you can do you can shift this statement inside if condition if it is happening then only it will be added so I am just shifting it here if you shift your addition of that instance inside the if then only if you update the stays then only error will come otherwise not because if stays is not updated your opportunity this OPP list to be updated will be blank and if it is blank so update won't be working so one more question is there for other stays it will create exception so it will it will be throwing error for closed one and closed lost for other opportunity stage update it won't break because of that condition so Archana if two triggers are checking for recursion so no trigger will be working at same time so at one time any one error will be working triggers won't work parallel and if you encounter this kind of situation so in that case you need to create two separate variables so in this class prevent recursion like you can create one more variable and that variable you can use like you will be having two variable and you can use them with two different triggers this way you can use it so Manan we are updating this opportunity so in case of update that data is already available there so you can see close date name these are already here we are just updating the stage we are just updating stays from the UI and we are updating the description from the code other data is already available so that's why it won't be breaking through close date so Praveen actually when we are creating this instance when we are assigning this ID so through that ID we are updating that record ID is same this ID we are passing into this O dot ID so we are not creating new opportunity we are updating the existing opportunity whose ID is this OPP dot ID so that's why we don't need to provide any required field here because through this ID we are updating the existing record only we are not like creating new record so if you assign existing ID into this newly created instance that means you are updating the existing record so Ravi Shankar if you see the error message then you can count I think it is it is around 16 16 times but you can count that error and that many times it shows the error you can also see these error message in the logs Kishore cybersecurity instructor instructor is Sumit Jain and that banner I shared in the telegram group and on LinkedIn you can search so I just have a LinkedIn post as well so I will be sharing that in the telegram group so you can get familiar so otherwise on 20 April instructor will be with me in the live session and he will be sharing his LinkedIn profile with you so that you can explore he's having 10 plus years of experience in the field of cybersecurity and ethical hacking yes Manan if you are if you are assigning ID to any instance so it means you are passing all the data like that that record you are passing ID and if you update that same record so it is updating that existing record so that that instance will be similar to that existing data existing record okay so I hope this way you understood the recursion process like how it is there and how you can prevent now moving forward we have one more scenario that we need to understand like how we can apply validation through add error method in Apex trigger so in in deletion in before deletion I already showed you like how we can apply validation but here like if we create any new record then at that time also we can apply validation so there are times like if you need to implement validation rule through code so how we can do that so there is a requirement like if you are creating new opportunity and at the time of opportunity creation if amount is null then we need to throw an error message like please populate amount on opportunity right so on opportunity I will be implementing a trigger and through that trigger I will be calling a method and that method will be validating like it it is a it is kind of a validation rule through code in simple words right so right now I'm checking the amount field only and we'll be throwing this error message so jumping here and here you can see I already implemented this trigger on opportunity so here writing before insert event so method name is validate amount and it is written inside before insert so that's why I am just passing trigger dot new right method name is validate amount so now I'm going to implement this method here for you so this is the method that I'm going to implement so I'm just saving this block so that I can save the trigger so trigger is being saved right now what will happen here I need to implement the code so it will be very simple code so we just need to iterate on OPP list and here I just need to check inside if OPP dot amount double equals to null so what I can do I can write OPP dot add error so add error is a method which will throw an error message if this condition is true so here I can write please populate amount on opportunity right so this way simply I implemented a custom validation here with the help of code and now if I create new opportunity and amount is null so I will see this error message on opportunity creation right so coming here and I'm going to create new opportunity so at the time of opportunity creation I'm writing test then close date is this stage is prospecting now you can see amount is blank I'm clicking on save so here you can see it is showing an error message please populate amount on opportunity and this error message is coming because of that custom validation so through code I am able to apply this validation and sometimes in the project we need to apply validation through code so this way you need to know like how we can throw an error message through this add error method right so it is it is very simple so we have two ways to apply validation rule one is through declarative stuff like through validation rule that we implemented implement through point and click and another is through this trigger you can also apply validation okay and if you go to my website study sales force and if you go to the scenario section here under trigger and test class you will find lots of scenarios and here you can see some validation related videos are available so this is for before delete before delete before update before insert before update so all these five are related to validation so you can practice these videos so total 22 scenarios are available here that you can practice all these scenarios are related to trigger and here like test classes are also implemented and I am planning to create some more videos that I will be adding in the list so let's see if I get some time so I will be creating more scenarios because after delete and after undelete scenarios are remaining so those I will be creating for okay so this way I hope you understood how we can apply validation in trigger now next we have another example which is related to database class and add error method so I'm going to implement it for you so add error method we already saw but if you are implementing insert operation or like if you if you are not implementing dml statement like insert update absurd delete so if you are not implementing them instead you are doing those dml operations with the help of database class methods so in in that case you can apply add error method as well right so there is a requirement that I'm going to show you so in dml statement what happens in case of dml statement so if I take you to the dml statement if you don't know the dml statement so basically this is a dml statement if you write this update and then list so this is known as dml statement if this dml is breaking if anything is breaking in this dml so whole process will be rolled back so for example if update is having 50 records and if one is failing so all other 49 will also fail so what what we do instead of writing dml we can write database class methods right and if database class method we are writing so if particular record is failing so that will fail other record will be like committed to the database and then after execution of that database class method we receive a list and we can apply a loop on that list and the records which are failed on that we can apply add error method right so that we can do with the help of this database class and add error method so here we have a requirement like when an account is created then create a related opportunity if there is any error while opportunity insert operation then through an error using adder method and insert the list using database class method so here in this example I'm not going to use any dml statement instead I will be using database class methods right because in dml statement if something is failing so whole process will be rolled back but in case of database class method it actually performs partial success and partial failure ok so this I need to implement on account because it says when account is created we need to create a related opportunity so whenever account is created we need to create related opportunity so here we have a method already implemented so I'm moving here so I'm going to search about that method that is create opportunity so it is available here so I'm going to modify this method so here in this method you can see this code is written this loop is available here so we will be using this loop as is now instead of this dml instead of dml statement what I'm going to do I will be writing database class method right in place of this insert OPP list I will be writing database class method ok so I have few lines of code written with me already so here I'm going to paste that code ok so what is happening here we are using database dot insert OPP list we are passing as a data data list and here I'm passing false so if you pass false as a parameter so what will happen if some records are breaking so only those will fail other will pass and here the result will be stored in this sr list so on this sr list size we are iterating the loop we are checking whether it is successful or not if it is not successful so here we are using not like if its success is false so what what we are doing we are implementing one more loop here database dot error err and through this sr list of I we are getting all the errors we are adding all the errors together and after completion of this loop on that acc list of I on that particular index where error is coming we are adding the errors so this way if you want to add error after that insert operation so this is the process and this is very much important so initially like in previous example what we did we implemented add error without dml operation but here after this insert operation we are adding the errors so this is the set code that you can use in your projects if you want to apply add error after insert or like update or delete operation so this way you can implement the loop and that error you can throw right so what you can you need to do like here I am commenting this stage and stage is required field but I am not populating it so whenever I will be creating new account and on account creation this trigger will be creating new opportunity and you can see new opportunity won't be created because here this statement is commented so through this code error message will be thrown right so now what I am going to do I am going to create an account record and you will see an error message here you can see ok so it is showing populate rating actually we implemented one trigger where rating should be available ok no no no it is basically for industry not for rating rating I am again setting as none it is for industry so industry I am selecting so if you remember we implemented a method update rating basis on the industry so if it is null then it is throwing an error so I am just clicking on save again it is throwing an error line number 74 before insert ok let me just check this error ok here actually we didn't apply that condition ACC industry not equals to null so if we don't apply that it is actually throwing the error so that's why null check is important so if that error is coming you need to fix the code so I just updated this code and here I am going to run it again ok so it is throwing some error so let me just comment it out because I just need to check why it is breaking so I just commented this insert one because it is running right now and here I am just clicking on save ok it is asking please populate amount on opportunity so that validation is working so what we need to do in this code we just need to populate the amount as well so here I am writing OPP dot amount so this value I am just saving it so right now stage is blank so let's see what happens now yeah so here you can see this showing required fields are missing stage name so this error is actually coming through that code which we implemented here right so this way one by one each error is checking and if you resolve your issues then your code will be working fine right so if you have any issue so you can just handle that with the help of this process after DML operation so instead of writing DML what you can do you can just use database class methods and then you can implement the error messages ok so I hope you understood this process like how we can use add error inside this process just after database dot insert method moving forward then we have trigger dot is executing so sometimes what happens you implement the code and that code is basically called through trigger as well as it is called through any other process as well so how we can check whether that code is called through trigger or not so we have this trigger dot is executing constructive context variable right so now I'm going to show you how we can implement this so in this account trigger handler I am just writing a code so public boolean handle account here I am writing list of account ok now here we can't use it so let me just implement it in the trigger itself account trigger handler so let me just write the code and then I will be doing some changes so trigger dot is executing so this way I'm just writing the code and if your code is executing through trigger so here you can write some code otherwise you can write the code in else ok so here do whatever you want to do as part of the trigger invocation and here right so if your code is called through trigger then you can write it otherwise else part will be executed let me see the error ok it is showing error for boolean so here what I can do I can just write return trigger dot is executing ok now if this method is called through trigger then this if block will be executed if it is called through any other process then it will be so let me just write it here ok so basically what I did I implemented this method where trigger dot new will be passed then we are just checking trigger dot is executing if it is called through trigger then this if part will be executed if it is called through without trigger then this part will be executed ok so now I'm going to call it through trigger so here I'm writing it so name is handle account ok it is returning a value as well so boolean b so actually what is happening this method is returning this boolean variable so you just need to store it somewhere so we need to create it as static so actually it is non static that's why this error is coming now it is resolved so you just need to create your method as static then only it can be called here so now it is error free so if I save it so it will be called ok and result we need to check in the logs so I will be calling I will be creating account ok before that what I'm going to do here I am going to uncomment this so that account will be created and opportunity will be created so I'm going to hit save so account is created and opportunity is created now I'm just moving to the logs and going to open it ok here we don't have any message moving forward opening another one debug only no message going to open another one debug only yeah here you can see trigger is executing and result is true through line number 7 if I go here at line number 7 so yeah so this is executing so actually right now this method is called through trigger so that's why this is executing line number 10 is not executing now you might be wondering how this line number 10 will be executed so from here if you call so here you can see we have this account list now what I need to do account trigger handler dot handle account and here I can pass ACC list and it is actually returning a boolean so boolean b equals to right so if I run this code logs will be opened and here you can see it is showing without trigger so this way I hope you understood if your code if your method is called through trigger and through without trigger and you just want to have different code so in that case this way you can just implement trigger dot is executing so one piece will be executed if your code is called through trigger and another piece will be executed if your code is called through without right so I hope this way you understood how this trigger dot is executing is working okay so and I think many for many of you it is new right so okay people are seeing account spelling mistake no it is not a mistake it is actually uppercase lowercase so if you like you all know it is case insensitive so if you write it in capital then also it will work fine so Revit in real time we prefer dml but if you have any doubt like if dml will be breaking still you want to commit the records which are successful so in that case you prefer database class so it depends on the scenario which you are implementing right next is trigger and order of execution so this I already explained to you so one more time I'm going to focus your I want to bring your focus on to this document so it is basically trigger and order of execution so whenever you insert update or absurd any statement any record so all these statements are executed step by step right so here you can see at line number four all before triggers are executed then record is saved to the database then all after trigger executed then assignment rule auto response rule workflow rule escalation rule other processes record trigger flows entitlement rules and here you are all dml statements operations will be committed to the database right so this is very much important so I'm just sharing this link in the chat so that you can go through with this document I already used this document in previous sessions as well so it is important if you are working with trigger or anything like if you are implementing any automation tool you need to know about this order of execution so it is not specific to trigger it is specific to all other things as well so trigger is a part and other things are also available here so you can just go and explore this document okay so this is it I planned for today's session so I hope you got lots of information after attending this session and share this channel link with people so that they can follow to bootcamp simultaneously so Salesforce bootcamp will go on in the way it is going on maybe from next week I will do little bit time change so I will be doing this session like one hour early so right now time is 8 30 p.m. IST but from next week it will be from 7 30 p.m. IST right because from 9 p.m. IST will be having cybersecurity bootcamp so this is this will be a little bit change for the Salesforce bootcamp rest all other things will be same right and I already shared the session trackers with you so we have two session tracker now one for Salesforce one for cybersecurity so whatever you are following you can go through and you will have all the information right I can see a few more questions so so Sachin in real time scenarios like if you have a method which you are calling through trigger as well as through any other process that is not a trigger so in the same method you can have if else and if trigger dot is executing is true it means it is called through trigger if it is false it means it is called through any other process so this way you can use that in the real time process within we won't be able to set the error message in the catch lock it is very difficult because we won't be able to iterate a loop there so actually we are applying these error messages for particular records which failed that can be done through database class only because it returns a list in try catch block you cannot do that thing okay so if you have any more question please ask otherwise I am going to end this session and tomorrow also we will be understanding few more examples and on Wednesday I will be explaining about apex and trigger best practices so Ankur Trivedi like apex testing sessions we will be doing next week so like agile methodology means if you are doing lots of processes together like few developers are doing development in a week and next week they are handling over all their development to the QA QAs like testing the requirements in the next week in the next week developments are also building new requirements so if a business analyst developer QA architect they are doing different different processes simultaneously and handling over like they are transferring their work done to the next phase so that is basically agile methodology so the benefit of agile methodology is like if you encounter any issue so immediately you can roll back the process so in waterfall model what used to happen so in waterfall model once all the development gets completed we used to go to testing phase but here in agile methodology we work in sprint form we divide the whole implementation into small small chunks like two to three weeks implementations so development testing all happens together ok Praveen is asking about wrapper class so I think it is not part of our tracker so I will have something around that as well yeah Praveen I just noted down your query will include that Vamshi this cyber security bootcamp session tracker is along with this Salesforce bootcamp tracker you can see there are two tabs so it is in the same sheet I don't need to share anything you can just refresh and you will have updated one yes when a backtracking is easy and agile because you don't implement whole thing and then go to next step things are working together so sort of I would prefer my this bootcamp so in bootcamp I explain each and everything from the beginning because I don't think anyone will be explaining in such a way so I just explain everything keeping in mind like you are a beginner so if you are not able to understand then I don't know any other resources because for beginner there there were no resource available in simple way that's why I am doing this bootcamp so that it can benefit beginners a lot ok guys so I don't see much questions so I think today's session is very fruitful and you learned a lot do practice all the scenarios because all the scenarios are different and very important so implement the code by watching the recording and understand and discuss the things in the telegram group and try to learn in the well way and tomorrow I will be completing all the top small small topics related to apex and trigger and day after tomorrow we will be having the best practice session so this week all these topics will be completed and next week we will be having fresh apex test class topics ok thank you so much for attending this session see you tomorrow same time so Sunil is asking about cyber security timing so I will update timings in the telegram groups right so don't worry about that thank you everybody see you tomorrow