 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. You can find all my Salesforce videos on this website so all the links are together available in this website so you can visit that. So in this video we are going to discuss about how we can prevent recursion in case of Apex trigger. So let me first explain you the scenario. So here you can see I have a trigger on account. So normally what happens when this recursion occurs mostly it occurs whenever you perform after update operation. So let's say you created one method update account and this method works when after update this after update event occurs. Right. So it means you are updating any record. Then this method should be called and if we move to this method which is available in this Apex trigger handler class. So if we move to this method. So in this method you will create that new list that created because of update operation on account. And after iterating this that list you are again creating one more list for account. Those to be updated. Right. So you updated one record that belongs to account. Then this method will be executed because of after update operation. Then inside this method you are creating one more list and you are again firing update operation. So what will happen this update will be again performed on account. So this after update trigger will be executed again and this method will be called again said then this will be again firing update DML. So this way this method will be executed again and again because you initiated update operation that update operation internally again updating account. So this will cause recursive calling of the trigger. Right. So let me show you how it looks like. So let's say this is an account and I'm updating this field. Right. So I'm doing an update operation clicking on save. So you will see a long error. So it started here and then you can see after update trigger event then after update after update so recursively it is calling again and again. Right. So now what we want to do whenever we are first updating our account record. So this list should be created and this should be updated. Right. And when we are applying this DML update DML inside this method then again this method won't be calling right this kind of a functionality we need to build. Right. So for this you can create one new class. So let's say I'm naming it as prevent recursion and here you can create public static Boolean first call equals to fall. So this is a Boolean variable whose name is first call and initially it is false. Right. So I'm saving this class and when we are calling this method here. So here you can apply if condition and inside this if condition you can call this I'm placing it here. Right. So initially it is false. So first time we want to execute this method. So that's why I put not here so it will become true. Right. And now I'm setting first call as because this if condition is true. So I'm setting this first call is true and calling this method. So this method will be called it will be iterating new list and will create a account to be updated list then again this DML will be fired. So as this DML will fire so again this trigger will be executed like this after update and here this will be checked if condition. So you can see this first call first call will be true because in last time we set as true and not will convert it into fall. So again this update account method won't be calling. Right. So here I need to put this as well because it is available in the class. Right. So this way, you can control recursion. This remember this thing this is only for one transaction. So if another transaction is performing. So in that case, this first call variable will be re initiated. So accordingly, operations will be performed. So now we are calling this update account inside this if condition according to the first call value. So I'm saving this account trigger. So it is saved now if I go here and if I click on save. So you will see no error and here you can see description is also updated. So one updated we did on did from UI and another update happened through the trigger. So this update account method and when this update account method updated account once again. So a second time this condition check. So it is false. So again this update account method was not calling. So that's why the person is prevented. So this way you can create one class and use this static variable initialize it as false. And then after calling this method first time you can make it as true so that you can avoid recursion. So this is a simple trick that you can apply to avoid recursion and mostly it happens whenever you apply after update operation. Right. So I hope you understood how we can prevent recursion in a big trigger with this demonstration. So if you want to watch more Salesforce related videos so all the videos those I created related to Salesforce their links are available in this website. So you can visit this website and you can learn Salesforce. Thank you for watching this video.