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)); } }
}
}