best way to handler trigger Recursive call
So as @cropredy suggested using static map instead this is what I tried. I used static set to solve this issue.
I can't say this is the best solution or not but this is working.
may me some usecases will be there please feel free to comment on this post.
So I used set of Id static variable.
Trigger
trigger RN_Account on Account (after update)
{
RN_AccountHandler handler = new RN_AccountHandler();
if(Trigger.isAfter && Trigger.isUpdate && !RN_AccountHandler.setAccountIds.containsAll(Trigger.newMap.KeySet()))
{
RN_AccountHandler.setAccountIds.addAll(Trigger.newMap.KeySet());
handler.onAfterUpdate(Trigger.newMap, Trigger.OldMap);
}
}
Handler
public with sharing class RN_AccountHandler
{
public static set<Id> setAccountIds = new set<Id>();
public void onAfterUpdate(map<Id, Account> mapNewAccount, map<Id, Account> mapOldAccount)
{
system.debug('====11========='+mapNewAccount.values().size());
update [SELECT Id, Name FROM Account WHERE Id IN: mapNewAccount.keyset()];
}
}
One more solution
IN controller after execution just make the boolean to true
Trigger
trigger RN_Account on Account (after update)
{
RN_AccountHandler handler = new RN_AccountHandler();
if(Trigger.isAfter && Trigger.isUpdate && RN_AccountHandler.isUpdate)
{
RN_AccountHandler.isUpdate = false;
handler.onAfterUpdate(Trigger.newMap, Trigger.OldMap);
}
}
Controller
public with sharing class RN_AccountHandler
{
public static Boolean isUpdate = true;
public void onAfterUpdate(map<Id, Account> mapNewAccount, map<Id, Account> mapOldAccount)
{
system.debug('============'+mapNewAccount.values().size());
update [SELECT Id, Name FROM Account WHERE Id IN: mapNewAccount.keyset()];
RN_AccountHandler.isUpdate = true;
}
}