Question
When a Billing Address is modified, get the new Postal Code. Then check which Contacts on the Account are outside that Postal Code. If 1 or more Contacts are outside of the Postal Code, mark Out_of_Zip as TRUE.
I want to update Out_of_Zip__c field on the Account when ever the above conditions are met but the problem is if after trigger is used then the trigger will goes to infinite loop and if before trigger is used is will show an DML exception.
Trigger code
trigger Update_Postal_Code on Account (after update) {
List<Account> lst_acc = [select id,Out_of_Zip__c,Billingpostalcode from Account where id in :Trigger.new];
for(Account a : lst_acc)
{
Out_of_Zip_Contact.Out_of_Zip(a);
}
}
Class code
public class Out_of_Zip_Contact {
Public static void Out_of_Zip(Account lst_acc){
list<Contact> lst_con = [select id,Mailingpostalcode from contact where Accountid =:lst_acc.id];
for(Contact c : lst_con)
{
if(c.MailingPostalCode == lst_acc.BillingPostalCode) {
continue;
}
else
{
lst_acc.Out_of_Zip__c = True ;
update lst_acc;
break;
}
}
}
}
This how I am trying to do but it shows me an error so please help to solve this issue.