Trigger..................
trigger AccountTriggerZip on Account (before update) {
if(Trigger.isBefore){
if(Trigger.isUpdate){
AccountTriggerZipHelper.checkZip(Trigger.new);
}
}
}
Helper Class..............
public class AccountTriggerZipHelper {
public static void checkZip(List<Account> updatedAccountList){
Account accountToUpdate;
List<Account> accountToUpdateList = new List<Account>(); // contains accounts
List<Account> acc = [SELECT id, Out_of_Zip__c, Billingpostalcode,
(SELECT id,Mailingpostalcode
FROM Contacts)
FROM Account WHERE id IN :updatedAccountList];
for(Account a : acc){
System.debug('Iterate Over Accounts');
for(Contact c : a.Contacts){
System.debug('Iterating over contacts');
if(c.MailingPostalCode == a.BillingPostalCode) {
}else{
accountToUpdate = new Account(Id = a.Id, Out_of_Zip__c=True);
}
}
}
if(accountToUpdateList.size()>0){
update accountToUpdateList;
}
}
}