Before using the code in this solution I would have trigger recursion where one trigger process called, Registration( which fires when Account is being inserted or before the acocunt is updated) would fire 10 times when Contact After Update trigger is fired once but there are 4 other triggers (i don;t have all their file names) that fire only once. When I used the KB solution 000199485 above I noticed, while looking at the debug log system.debug statements, that the Registration trigger fires only once (which is good) but the other 4 triggers don't get to run. To solve this , I set the static boolean flag - firstRun - to true after the Trigger's IF Conditional has been executed and finished. Therefore when I re-fire the Contact After Update trigger , all the trigger processes fire once per trigger and no more.
Q) Is this the correct/intended result? Here is a code sample of what I mean and pay special attention to the
checkRecursive.isResetRun() method call:
trigger AccountAfterUpdate on Account (after update) {
if(checkRecursive.isFirstRun()){ // To avoid the recursion on trigger, make sure your trigger is getting executed only one time and lock this ciritcal section of code. One Trigger Per Object
Integer acctLimit = Trigger.new.size();
List<Account> UpdateAccountList = new List<Account>();
List<Account> UpdateRegLevelList = new List<Account>();
List<ID> acctIDs = new List<ID>();
List<Account> clonedAccounts = new List<Account>();
Account modifiedAccount;
Account clonedAccount;
List<RecordType> getPersonRecordType = [Select Id from recordType where Name = 'Individual_Person'];
List<RecordType> getUSRecordType = [Select Id from recordType where Name = 'US Organization'];
List<Account> OtherAccounts = new List<Account>(); //non-PMM Account tasks.
//1. See if its PMM value has changed for Account records.
for(Integer aCnt = 0; aCnt < acctLimit; aCnt++){
//A. <<For Address Synchronization:>> Check to see if Address have been updated for Person Accounts.... 11-29-2014
if(Trigger.new[aCnt].RecordTypeId == getPersonRecordType[0].Id){
if(Trigger.new[aCnt].ShippingStreet != Trigger.old[aCnt].ShippingStreet ||
Trigger.new[aCnt].ShippingCity != Trigger.old[aCnt].ShippingCity ||
Trigger.new[aCnt].ShippingState != Trigger.old[aCnt].ShippingState ||
Trigger.new[aCnt].ShippingCountry != Trigger.old[aCnt].ShippingCountry ||
Trigger.new[aCnt].ShippingPostalCode != Trigger.old[aCnt].ShippingPostalCode ||
Trigger.new[aCnt].BillingStreet != Trigger.old[aCnt].BillingStreet ||
Trigger.new[aCnt].BillingCity != Trigger.old[aCnt].BillingCity ||
Trigger.new[aCnt].BillingState != Trigger.old[aCnt].BillingState ||
Trigger.new[aCnt].BillingCountry != Trigger.old[aCnt].BillingCountry ||
Trigger.new[aCnt].BillingPostalCode != Trigger.old[aCnt].BillingPostalCode
){
modifiedAccount = Trigger.new[aCnt];
clonedAccount = new Account(Id=modifiedAccount.Id, Physical_Street_1__c =modifiedAccount.BillingStreet, Physical_City__c = modifiedAccount.BillingCity,
Physical_Zip__c = modifiedAccount.BillingPostalCode,Physical_State__c = modifiedAccount.BillingState,
PhysicalCountry__c = modifiedAccount.BillingCountry,Mailing_Street_1__c = modifiedAccount.ShippingStreet,
Mailing_City__c = modifiedAccount.ShippingCity,Mailing_State_Province__c = modifiedAccount.ShippingState,
Mailing_Zip_Postal_Code__c = modifiedAccount.ShippingPostalCode,Mailing_Country__c = modifiedAccount.ShippingCountry);
clonedAccounts.add(clonedAccount);
}
}
//B.<<For Team Member Manager:>> Check to see if the Owner Id has changed on US Organizations
if(Trigger.new[aCnt].RecordTypeId == getUSRecordType[0].Id){ //11-23-2014 Added conditional using Record Type as a flag to process for US Orgs only.
if(Trigger.new[aCnt].OwnerId != Trigger.old[aCnt].OwnerId && (Trigger.new[aCnt].OwnerId != null && Trigger.old[aCnt].OwnerId != null) ){ // This should never happen for Person Accounts.
UpdateAccountList.add(Trigger.new[aCnt]);
acctIDs.add(Trigger.new[aCnt].Id);
}//IF
}//IF
}// FOR
//2. <<For Team Member Manager:>> Displatch for processing which accounts ..
if(UpdateAccountList.size() > 0){
system.debug('<<TMACCTS>> '+UpdateAccountList);
TeamMemberAccounts.handler(UpdateAccountList,acctIDs);
}
//3. <<For Address Synchronization on Person Accounts:>>
if(clonedAccounts.size() > 0 ){
update clonedAccounts;
system.debug('*** New ACCOUNT ID *** '+clonedAccounts[0].Id+' *** New ACCOUNT Mailingn Address *** '+clonedAccounts[0].Mailing_Street_1__c+' *** New ACCOUNT PhysicalAddress *** '+clonedAccounts[0].Physical_Street_1__c);
}
}// checkRecursion
checkRecursive.isResetRun(); // Before leaving, give 'key' to the next available trigger to unlock critical section and allow Access.
}
Class code for the KB is basically the same:
public class checkRecursive{
private static boolean firstRun = true;
public static boolean isFirstRun(){
if(firstRun){
firstRun = false;
return true;
}else{
return firstRun;
}
}
public static boolean isResetRun(){
firstRun = true;
return true;
}
}// checkRecursive
Thanks for your help comments and questions. I just want to make sure that either I fixed the KB or if I have created any additional problems for myself . With my solution the 4 trigger processes only fire once after a contact has been updated.
This Answers Community is focused on configuration and design questions. Programmatic questions are best submitted to the developer forums at https://developer.salesforce.com where the forums and participants are geared toward programming troubleshooting and support.