You need to sign in to do that
Don't have an account?
Cascading trigger
In a custom object I have a checkbox that I want to be unique for a group of records based on a relation. In example for all the records related to the same account, I want only one to have this field checked.
To do so I implemented a trigger that would uncheck all the other records after new one is checked and also update a field in the account record; when the record is unchecked, I clear the field in the account.
The problem I have is that this provoke triggers on the modified records as well, now how do I know if the trigger is the original one or a cascaded one?
Use a public static variable in a common Class to differentiate between Triggers. One trigger sets the variable , and the other can check its value.
For example:
public class utilClass { public static boolean triggerVariable = true;
}
trigger AccountTest on Account (before insert, before update) { if(utilClass.triggerVariable == true){ //Do some logic //Set the variable to true so the other trigger's can check its value. utilClass.triggerVariable = false; } }
trigger ContactTrigger on Contact (before insert, before update) { if(utilClass.triggerVariable = false){ //Do some logic //Set the variable to true so the other trigger's can check its value. utilClass.triggerVariable = true; } }
All Answers
Use a public static variable in a common Class to differentiate between Triggers. One trigger sets the variable , and the other can check its value.
For example:
public class utilClass { public static boolean triggerVariable = true;
}
trigger AccountTest on Account (before insert, before update) { if(utilClass.triggerVariable == true){ //Do some logic //Set the variable to true so the other trigger's can check its value. utilClass.triggerVariable = false; } }
trigger ContactTrigger on Contact (before insert, before update) { if(utilClass.triggerVariable = false){ //Do some logic //Set the variable to true so the other trigger's can check its value. utilClass.triggerVariable = true; } }
Works thanks, just had to add an extra local variable to unset the static after the update if I was the originator.