Below are the two primary scenarios:
1) Many Developers face recursive trigger , or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.
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.
We 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 isFirstOccurance = true;
}
Trigger Code
trigger ContactTriggers on Contact (after update)
{
Set<String> accIdSet = new Set<String>();
if(ContactTriggerHandler. isFirstOccurance)
{
ContactTriggerHandler. isFirstOccurance = false;
for(Contact conObj : Trigger.New)
{
if(conObj.name != 'Test')
{
accIdSet.add(conObj.accountId);
}
}
// any code here
}
}
1) Many Developers face recursive trigger , or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.
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.
We 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 isFirstOccurance = true;
}
Trigger Code
trigger ContactTriggers on Contact (after update)
{
Set<String> accIdSet = new Set<String>();
if(ContactTriggerHandler. isFirstOccurance)
{
ContactTriggerHandler. isFirstOccurance = false;
for(Contact conObj : Trigger.New)
{
if(conObj.name != 'Test')
{
accIdSet.add(conObj.accountId);
}
}
// any code here
}
}
No comments:
Post a Comment