I am trying to stop a cyclic situation between two triggers. Trigger B is the primary - it runs when an Opp is edited (to put some data on the ACCOUNT). Trigger A was written so that if the Account is edited or merged, it edit/saves each Opportunity afterward, causing a cleanup of the data on the Account (especially after merges).
Because A is fired by an edit on Account, and B touches the Account at the end of the process, A fires B fires A... etc...
I tried setting a hidden checkbox field and then unsetting it in an IF in Trigger A, but the record is LOCKED at the time so I cannot clear the checkbox (else this would solve my problem - first time, A fires B, which fires A but the set checkbox skips firing B then clears the checkbox - which in itself would ALSO PROBABLY be an issue, but I digress...).
So it was suggested that I create a Static copy of the Accounts being touched in Trigger A, and then when it fires "again", that list could be used to exclude them. I need help with this - I have no clue how to create a Static reference, especially with a "Map" set. :-(
Note the code below in Trigger A:
Account[] acctsInTriggerSet = new List<Account>();
Opportunity[] opptysToUpdate= new List<Opportunity>();
if( PiggybackTriggerManager.PBOppFromAccountCheck == true ){
//Do nothing, the calculation is done
} else {
for(Account a :Trigger.new){
acctsInTriggerSet.add(a);
}
For( Opportunity o : [SELECT id, Name
FROM Opportunity
WHERE Accountid
IN :acctsInTriggerSet] ) {
// o.Name = o.Name;
opptysToUpdate.add(o); }
update opptysToUpdate;
}
...and in Trigger B (relevant code):
if( !updateMap.containsKey((String)accObj.get('Id') ) ) { //check that Acc Id not already added
updateMap.put((String)accObj.get('Id'), (Account)accObj);
if(updateMap.size() >= 199) {
Database.update( updateMap.values() );
}
}
....and later for the "remainders"......
Database.update( updateMap.values() );
PiggybackTriggerManager.PBOppFromAccountCheck = true;
So what I'd be wanting is a second "Map" set that is Static and could be checked while in the first FOR loop. If the item being set with .add(a) is already in that Static set, it would NOT add it. Help?!