function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KMNKMN 

When a Billing Address is modified, get the new Postal Code.

Pre-Reqs:
Create a field on Account called “Out_of_Zip”, checkbox, default off

Assignment:
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.

This code is in Nasted loop how to avoid nasted loop and make this code with Map
Trigger..................

trigger AccoounTriggerZip on Account (before update) {
    if(Trigger.isBefore){
        if(Trigger.isUpdate){
            AccoounTriggerZipHelper.checkZip(Trigger.new);
        }
    }
}

Helper Class..............

public class AccoounTriggerZipHelper {
    Static Boolean firsCall = True;  // usig flag to avoid recursive trigger
    public static void checkZip(List<Account> updatedAccountList){
        Account accountToUpdate;
        List<Account> accountToUpdateList; // contains accounts
        List<Account> acc = [SELECT id, Out_of_Zip__c, Billingpostalcode,
                             (SELECT id,Mailingpostalcode 
                                        FROM Contacts)
                                    FROM Account WHERE id IN :updatedAccountList];
        accountToUpdateList = new List<Account>();
        if(firsCall == True){
            for(Account a : acc){
                System.debug('Iterate Over Accounts');
                for(Contact c : a.Contacts){
                    System.debug('Iterating over contacts');
                    if(c.MailingPostalCode == a.BillingPostalCode) {
                        continue;
                    }else{
                       accountToUpdate = new  Account(Id = a.Id, Out_of_Zip__c=True);
                    }
                }
            }
        }
        if(accountToUpdateList.size()>0){
            firsCall = False;            // this will not call again
            update accountToUpdateList;
        }
    }
}
Suraj Tripathi 47Suraj Tripathi 47
Hi Khalid Ameen,
You can use this code, it is working according your requirement :
Trigger-->
trigger AccoounTriggerZip on Account (before update) {
    if(Trigger.isbefore){
        if(Trigger.isUpdate){
            AccoounTriggerZipHelper.checkZip(Trigger.new);
        }
    }
}

Handler-->
public class AccoounTriggerZipHelper {  
    public static void checkZip(List<Account> updatedAccountList){
        List<account> accList = new List<account>();
        set<id> setAccId = new set<id>();
        System.debug('->'+setAccId);
        for(account acc : updatedAccountList){
            setAccId.add(acc.id);
        }
        List<contact> conList = new List<contact>();
        conList = [select id, lastName, accountId,MailingPostalCode from contact where accountId In : setAccId];
        System.debug('contact List -->'+conList);
        for(account acc:updatedAccountList){
            for(contact con:conList){
                if(acc.Id == con.accountId){
                    if(acc.BillingPostalCode != con.MailingPostalCode){
                        
                        acc.Out_of_Zip__c = true ;
                    }
                }
            }
        }
    }
}

If you find your Solution then mark this as the best answer.
 Thank you!
 Regards,
 Suraj Tripathi
KMNKMN
HI Suraj,
In your code you are also using loop inside the loop..
just that thing i want to avoid so how can it be possible
Venu GopalVenu Gopal
Hi Khalid Ameen,
Trigger
trigger Outofzip on Account (before update) {
    if(trigger.isbefore && trigger.isupdate){
        AccountOutofZipHandler.checkzip(trigger.newmap);
    }
}

Helper Class:-
public class AccountOutofZipHandler {
    
    public static void checkzip(map<id,account> accountlist)
    {
      for(contact c: [select accountid, MailingPostalCode from contact                 
                        where accountid in:accountlist.keyset()])
        {
            account a=accountlist.get(c.accountid);
            
            if(a.BillingPostalCode!=c.MailingPostalCode){
                a.Out_of_Zip__c=true;
            }
        }
    }

Hope this answer helps!
Venu