Skip to main content
trigger moveContactsOutOfView on Account (After Update) {
    if(checkRecursive.runOnce()){

set notAccId = New Set();
set accId = New Set(); List conUpdate = new List(); List con1Update = new list(); List d1 = new List();


        set<String> notAccId = New Set<String>();     
        set<String> accId = New Set<string>();
        List<Contact> conUpdate = new List<Contact>();
        List<Contact> con1Update = new list<Contact>();
        List<AccountTeamMember> d1 = new List<AccountTeamMember>();

        for (Account a1: trigger.new){
            if (a1.ID != null && a1.Move_out_of_view__c == true && a1.LastModifiedById != '00500000006xZUv' && a1.LastModifiedById != '00500000006wsIm' && a1.LastModifiedById != '00500000006xdMm' && a1.LastModifiedById != '00500000006wnoE'){
                accId.Add(a1.ID);
            }
            else if(a1.ID != null && a1.Move_out_of_view__c == false && a1.LastModifiedById != '00500000006xZUv' && a1.LastModifiedById != '00500000006wsIm' && a1.LastModifiedById != '00500000006xdMm' && a1.LastModifiedById != '00500000006wnoE'){
                notAccId.add(a1.ID);
    
         }
        }
                               

LIST cY = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: AccId]; LIST cN = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: notAccId]; List toBeDeleted =[SELECT ID, LastModifiedById FROM AccountTeamMember WHERE AccountID In: AccId];

        LIST<Contact> cY = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: AccId];
        LIST<Contact> cN = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: notAccId];
        List<AccountTeamMember> toBeDeleted =[SELECT ID, LastModifiedById FROM AccountTeamMember WHERE AccountID In: AccId];

        for (Account a1: trigger.new){
            for (Contact c1:cY){
                if (c1.LastModifiedById != '00500000006xdMm' && c1.LastModifiedById != '00500000006wnoE' && c1.LastModifiedById != '00500000006wsIm' && c1.LastModifiedById != '00500000006xZUv'){
                    c1.Move_out_of_view__c = True;
                    conUpdate.add(c1);
                }
            }
            for (Contact c2:cN){
                if (c2.LastModifiedById != '00500000006xdMm'&& c2.LastModifiedById != '00500000006wnoE' && c2.LastModifiedById != '00500000006wsIm' && c2.LastModifiedById != '00500000006xZUv'){
                    c2.Move_out_of_view__c = False;
                    con1Update.add(c2);
                }
            }
        }
        for (AccountTeamMember t1:toBeDeleted){
            if (t1.LastModifiedById != '00500000006xdMm' && t1.LastModifiedById != '00500000006wnoE' && t1.LastModifiedById != '00500000006wsIm' && t1.LastModifiedById != '00500000006xZUv'){
                d1.add(t1);
            }
        }

        try{ 
            update conUpdate;
        }
        catch (DMLException e) {
            for (Account con : trigger.new) {
                con.addError(e.getDmlMessage(0));
            }
        }
        try{
            update con1Update;
        }
        catch (DMLException a){
            for (Account acc:trigger.new){
            acc.addError(a.getDmlMessage(0));
        }
    }
    try{
        delete d1;
    }
    catch (DMLException b){
        for (Account acc1:trigger.new){
            acc1.addError(b.getDmlMessage(0));
        }
    }    
}
//}

try{ update conUpdate; }

catch (DMLException e) { for (Account con : trigger.new) { con.addError(e.getDmlMessage(0)); } } try{ update con1Update; } catch (DMLException a){ for (Account acc:trigger.new){ acc.addError(a.getDmlMessage(0)); } } try{ delete d1; } catch (DMLException b){ for (Account acc1:trigger.new){ acc1.addError(b.getDmlMessage(0)); } }

}

}

trigger moveContactsOutOfView on Account (After Update) {
  if(checkRecursive.runOnce()){

set notAccId = New Set();
set accId = New Set(); List conUpdate = new List(); List con1Update = new list(); List d1 = new List();

  for (Account a1: trigger.new){
  if (a1.ID != null && a1.Move_out_of_view__c == true && a1.LastModifiedById != '00500000006xZUv' && a1.LastModifiedById != '00500000006wsIm' && a1.LastModifiedById != '00500000006xdMm' && a1.LastModifiedById != '00500000006wnoE'){
      accId.Add(a1.ID);
      }
  else if(a1.ID != null && a1.Move_out_of_view__c == false && a1.LastModifiedById != '00500000006xZUv' && a1.LastModifiedById != '00500000006wsIm' && a1.LastModifiedById != '00500000006xdMm' && a1.LastModifiedById != '00500000006wnoE'){
    notAccId.add(a1.ID);
    
     }
  }
                               

LIST cY = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: AccId]; LIST cN = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: notAccId]; List toBeDeleted =[SELECT ID, LastModifiedById FROM AccountTeamMember WHERE AccountID In: AccId];

   for (Account a1: trigger.new){
        for (Contact c1:cY){
            if (c1.LastModifiedById != '00500000006xdMm' && c1.LastModifiedById != '00500000006wnoE' && c1.LastModifiedById != '00500000006wsIm' && c1.LastModifiedById != '00500000006xZUv'){
                c1.Move_out_of_view__c = True;
                conUpdate.add(c1);
            }
        }
        for (Contact c2:cN){
            if (c2.LastModifiedById != '00500000006xdMm'&& c2.LastModifiedById != '00500000006wnoE' && c2.LastModifiedById != '00500000006wsIm' && c2.LastModifiedById != '00500000006xZUv'){
                c2.Move_out_of_view__c = False;
                con1Update.add(c2);
            }
        }
   }
      for (AccountTeamMember t1:toBeDeleted){
            if (t1.LastModifiedById != '00500000006xdMm' && t1.LastModifiedById != '00500000006wnoE' && t1.LastModifiedById != '00500000006wsIm' && t1.LastModifiedById != '00500000006xZUv'){
                d1.add(t1);
            }
      }

try{ update conUpdate; }

catch (DMLException e) { for (Account con : trigger.new) { con.addError(e.getDmlMessage(0)); } } try{ update con1Update; } catch (DMLException a){ for (Account acc:trigger.new){ acc.addError(a.getDmlMessage(0)); } } try{ delete d1; } catch (DMLException b){ for (Account acc1:trigger.new){ acc1.addError(b.getDmlMessage(0)); } }

}

}

trigger moveContactsOutOfView on Account (After Update) {
    if(checkRecursive.runOnce()){

        set<String> notAccId = New Set<String>();     
        set<String> accId = New Set<string>();
        List<Contact> conUpdate = new List<Contact>();
        List<Contact> con1Update = new list<Contact>();
        List<AccountTeamMember> d1 = new List<AccountTeamMember>();

        for (Account a1: trigger.new){
            if (a1.ID != null && a1.Move_out_of_view__c == true && a1.LastModifiedById != '00500000006xZUv' && a1.LastModifiedById != '00500000006wsIm' && a1.LastModifiedById != '00500000006xdMm' && a1.LastModifiedById != '00500000006wnoE'){
                accId.Add(a1.ID);
            }
            else if(a1.ID != null && a1.Move_out_of_view__c == false && a1.LastModifiedById != '00500000006xZUv' && a1.LastModifiedById != '00500000006wsIm' && a1.LastModifiedById != '00500000006xdMm' && a1.LastModifiedById != '00500000006wnoE'){
                notAccId.add(a1.ID);
            }
        }
                               
        LIST<Contact> cY = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: AccId];
        LIST<Contact> cN = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: notAccId];
        List<AccountTeamMember> toBeDeleted =[SELECT ID, LastModifiedById FROM AccountTeamMember WHERE AccountID In: AccId];

        for (Account a1: trigger.new){
            for (Contact c1:cY){
                if (c1.LastModifiedById != '00500000006xdMm' && c1.LastModifiedById != '00500000006wnoE' && c1.LastModifiedById != '00500000006wsIm' && c1.LastModifiedById != '00500000006xZUv'){
                    c1.Move_out_of_view__c = True;
                    conUpdate.add(c1);
                }
            }
            for (Contact c2:cN){
                if (c2.LastModifiedById != '00500000006xdMm'&& c2.LastModifiedById != '00500000006wnoE' && c2.LastModifiedById != '00500000006wsIm' && c2.LastModifiedById != '00500000006xZUv'){
                    c2.Move_out_of_view__c = False;
                    con1Update.add(c2);
                }
            }
        }
        for (AccountTeamMember t1:toBeDeleted){
            if (t1.LastModifiedById != '00500000006xdMm' && t1.LastModifiedById != '00500000006wnoE' && t1.LastModifiedById != '00500000006wsIm' && t1.LastModifiedById != '00500000006xZUv'){
                d1.add(t1);
            }
        }

        try{ 
            update conUpdate;
        }
        catch (DMLException e) {
            for (Account con : trigger.new) {
                con.addError(e.getDmlMessage(0));
            }
        }
        try{
            update con1Update;
        }
        catch (DMLException a){
            for (Account acc:trigger.new){
            acc.addError(a.getDmlMessage(0));
        }
    }
    try{
        delete d1;
    }
    catch (DMLException b){
        for (Account acc1:trigger.new){
            acc1.addError(b.getDmlMessage(0));
        }
    }    
}
//}
Source Link

Apex Trigger on Account working properly in Sandbox but does nothing in Production

I have written a few triggers for my company's org, all seems to be working correctly except one on Account... It was working for the last few months and just today, I have noticed that it stopped working. There is no error message, it just does nothing.

The code coverage for this code is 100% given the test class is in place.

This is an "After" trigger, in which I have switched to "Before" and again, it works in Sandbox, but does nothing in Production... So I have switched it back to the original state when it worked the first few months after I have deployed it into Production. I have also checked and made sure that all custom validation rules on Accounts and Contacts are mirroring each other between Sandbox and Production.

See code below. Any help or explanation is greatly appreciated!!! Thanks in advance!

trigger moveContactsOutOfView on Account (After Update) {
  if(checkRecursive.runOnce()){

set notAccId = New Set();
set accId = New Set(); List conUpdate = new List(); List con1Update = new list(); List d1 = new List();

  for (Account a1: trigger.new){
  if (a1.ID != null && a1.Move_out_of_view__c == true && a1.LastModifiedById != '00500000006xZUv' && a1.LastModifiedById != '00500000006wsIm' && a1.LastModifiedById != '00500000006xdMm' && a1.LastModifiedById != '00500000006wnoE'){
      accId.Add(a1.ID);
      }
  else if(a1.ID != null && a1.Move_out_of_view__c == false && a1.LastModifiedById != '00500000006xZUv' && a1.LastModifiedById != '00500000006wsIm' && a1.LastModifiedById != '00500000006xdMm' && a1.LastModifiedById != '00500000006wnoE'){
    notAccId.add(a1.ID);
    
    }
  }
                               

LIST cY = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: AccId]; LIST cN = [SELECT AccountID, LastModifiedById FROM Contact WHERE AccountID In: notAccId]; List toBeDeleted =[SELECT ID, LastModifiedById FROM AccountTeamMember WHERE AccountID In: AccId];

   for (Account a1: trigger.new){
        for (Contact c1:cY){
            if (c1.LastModifiedById != '00500000006xdMm' && c1.LastModifiedById != '00500000006wnoE' && c1.LastModifiedById != '00500000006wsIm' && c1.LastModifiedById != '00500000006xZUv'){
                c1.Move_out_of_view__c = True;
                conUpdate.add(c1);
            }
        }
        for (Contact c2:cN){
            if (c2.LastModifiedById != '00500000006xdMm'&& c2.LastModifiedById != '00500000006wnoE' && c2.LastModifiedById != '00500000006wsIm' && c2.LastModifiedById != '00500000006xZUv'){
                c2.Move_out_of_view__c = False;
                con1Update.add(c2);
            }
        }
   }
      for (AccountTeamMember t1:toBeDeleted){
            if (t1.LastModifiedById != '00500000006xdMm' && t1.LastModifiedById != '00500000006wnoE' && t1.LastModifiedById != '00500000006wsIm' && t1.LastModifiedById != '00500000006xZUv'){
                d1.add(t1);
            }
      }

try{ update conUpdate; }

catch (DMLException e) { for (Account con : trigger.new) { con.addError(e.getDmlMessage(0)); } } try{ update con1Update; } catch (DMLException a){ for (Account acc:trigger.new){ acc.addError(a.getDmlMessage(0)); } } try{ delete d1; } catch (DMLException b){ for (Account acc1:trigger.new){ acc1.addError(b.getDmlMessage(0)); } }

}

}