Problem :-
2) You want to write a trigger that creates a new record ; however, that record may then cause another trigger to fire, which in turn causes another to fire, and so on.
Solution :-
you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.
Apex Class with Static Variable public class ContactTriggerHandler
{
public static Boolean isFirstTime = true;
}
Trigger Codetrigger ContactTriggers on Contact (after update)
{
Set<String> accIdSet = new Set<String>();
if(ContactTriggerHandler.isFirstTime)
{
ContactTriggerHandler.isFirstTime = false;
for(Contact conObj : Trigger.New)
{
if(conObj.name != 'Test')
{
accIdSet.add(conObj.accountId);
}
}
// any code here
}
}
This comment has been removed by the author.
ReplyDeleteDo we need to create a test class as well for this
ReplyDeleteits a solution for one record update, what about bulk records?
ReplyDeletewhat if the trigger runs for before and after event .
ReplyDeleteIf the before trigger runs the static variable will be changed already and then after triggers runs after trigger wont work ?
Thank you! This was extremely helpful.
ReplyDelete