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
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI 

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.

Colton WehkingColton Wehking
This code should do that, optionally put it into a trigger handler.
trigger TriggerExample on Account (before update, before insert) {
    Set<Id> changedAccounts = new Set<Id>();
    for(Integer i = 0; i < Trigger.New.size(); i++) {
        Account newAcc = Trigger.New[i];
        Account oldAcc = Trigger.Old[i];
        
        if(newAcc.BillingStreet != oldAcc.BillingStreet ||
          newAcc.BillingCity != oldAcc.BillingCity ||
          newAcc.BillingState != oldAcc.BillingState ||
          newAcc.BillingPostalCode != oldAcc.BillingPostalCode ||
          newAcc.BillingCountry != oldAcc.BillingCountry)
              changedAccounts.add(newAcc.Id);
        
        Map<Id, Integer> accountsAndOutOfZips = new Map<Id, Integer>();
        for(Contact c : [SELECT Id, Account.BillingPostalCode FROM Contact WHERE Id IN :changedAccounts]) {
            if(c.MailingPostalCode != c.Account.BillingPostalCode) {
                if(accountsAndOutOfZips.get(c.Id) == null)
                    accountsAndOutOfZips.put(c.Id, 1);
                else
                    accountsAndOutOfZips.put(c.Id, accountsAndOutOfZips.get(c.Id) + 1);
            }
        }
        
        for(Account acc : Trigger.New) {
            if(accountsAndOutOfZips.get(acc.Id) != null && accountsAndOutOfZips.get(acc.Id) > 1)
                acc.Out_of_Zip__c = true;
            else
                acc.Out_of_Zip__c = false;
        }
    }
}

 
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
but why are we using before insert and before update in this case?
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
will you please elaborate the working of this code
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
code not working
 
Colton WehkingColton Wehking
You will encounter an error if you attempt to put this in an after insert/update as you can not update the record that caused a trigger to fire in that context.

The code starts by looping through all accounts that have been updated, then checks if any of them have had their address changed (either city, street, state, etc.). The ones that have had their address changed are added to the list called changedAccounts. Next, we query for all the contacts that belong to those accounts and loop through them. If any of them have a postal code that doesn't match what the account has, incremement a counter in the Map accountsAndOutOfZips. That just keeps track of how many contacts have mismatched postal codes for each account. In the final for loop, we go through each account and check if any of them have more than 1 contact that didn't have the same postal code. If any of them had more, we set Out Of Zip to true, otherwise it's set to false.

Regarding the error, can you share what error you are receiving?
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
cant i do this without using nested loop?
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
i have to do this task using handlers only so how can i do that with best practice?
 
Colton WehkingColton Wehking
My bad, missed a '}' in my snippet. Shouldn't have a nested loop in it.
trigger TriggerExample on Account(before update, before insert) {
    Set < Id > changedAccounts = new Set < Id > ();
    for (Integer i = 0; i < Trigger.New.size(); i++) {
        Account newAcc = Trigger.New[i];
        Account oldAcc = Trigger.Old[i];

        if (newAcc.BillingStreet != oldAcc.BillingStreet ||
            newAcc.BillingCity != oldAcc.BillingCity ||
            newAcc.BillingState != oldAcc.BillingState ||
            newAcc.BillingPostalCode != oldAcc.BillingPostalCode ||
            newAcc.BillingCountry != oldAcc.BillingCountry)
            changedAccounts.add(newAcc.Id);
    }
    Map < Id, Integer > accountsAndOutOfZips = new Map < Id, Integer > ();
    for (Contact c: [SELECT Id, Account.BillingPostalCode FROM Contact WHERE Id IN: changedAccounts]) {
        if (c.MailingPostalCode != c.Account.BillingPostalCode) {
            if (accountsAndOutOfZips.get(c.Id) == null)
                accountsAndOutOfZips.put(c.Id, 1);
            else
                accountsAndOutOfZips.put(c.Id, accountsAndOutOfZips.get(c.Id) + 1);
        }
    }

    for (Account acc: Trigger.New) {
        if (accountsAndOutOfZips.get(acc.Id) != null && accountsAndOutOfZips.get(acc.Id) > 1)
            acc.Out_of_Zip__c = true;
        else
            acc.Out_of_Zip__c = false;
    }
}

You can find many trigger handler frameworks online, but a simple one would be something like
 
trigger TriggerExample on Account(before update, before insert, before delete, after update, after insert, after delete) {
    TestTriggerHandler testTriggerHandler = new TestTriggerHandler (Trigger.new, Trigger.old);
    
    /* Before Insert */
    if(Trigger.isInsert && Trigger.isBefore){      
        testTriggerHandler.yourMethod();
    }
    
    /* After Insert */
    else if(Trigger.isInsert && Trigger.isAfter){

    }
    /* Before Update */
    else if(Trigger.isUpdate && Trigger.isBefore){
        testTriggerHandler.yourMethod();
    }
    /* After Update */
    else if(Trigger.isUpdate && Trigger.isAfter){
        blacklistTriggerHandler.updateAppointments();
    }
    /* Before Delete */
    else if(Trigger.isDelete && Trigger.isBefore){

    }
    /* After Delete */
    else if(Trigger.isDelete && Trigger.isAfter){

    }

    /* After Undelete */
    else if(Trigger.isUnDelete){

    }
}

Then move the code to it's own class. In this case, I'm calling it TestTriggerHandler
public class TestTrigger Handler {
public void yourMethod(){
Set < Id > changedAccounts = new Set < Id > ();
    for (Integer i = 0; i < Trigger.New.size(); i++) {
        Account newAcc = Trigger.New[i];
        Account oldAcc = Trigger.Old[i];

        if (newAcc.BillingStreet != oldAcc.BillingStreet ||
            newAcc.BillingCity != oldAcc.BillingCity ||
            newAcc.BillingState != oldAcc.BillingState ||
            newAcc.BillingPostalCode != oldAcc.BillingPostalCode ||
            newAcc.BillingCountry != oldAcc.BillingCountry)
            changedAccounts.add(newAcc.Id);
    }
    Map < Id, Integer > accountsAndOutOfZips = new Map < Id, Integer > ();
    for (Contact c: [SELECT Id, Account.BillingPostalCode FROM Contact WHERE Id IN: changedAccounts]) {
        if (c.MailingPostalCode != c.Account.BillingPostalCode) {
            if (accountsAndOutOfZips.get(c.Id) == null)
                accountsAndOutOfZips.put(c.Id, 1);
            else
                accountsAndOutOfZips.put(c.Id, accountsAndOutOfZips.get(c.Id) + 1);
        }
    }

    for (Account acc: Trigger.New) {
        if (accountsAndOutOfZips.get(acc.Id) != null && accountsAndOutOfZips.get(acc.Id) > 1)
            acc.Out_of_Zip__c = true;
        else
            acc.Out_of_Zip__c = false;
    }
}
}

 
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
THANKS A MILLION COLTON
 
SUYASH KUMAR BHARTISUYASH KUMAR BHARTI
Greetings Colton,
how can i do reparenting of above code