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.
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?
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?
You can find many trigger handler frameworks online, but a simple one would be something like
Then move the code to it's own class. In this case, I'm calling it TestTriggerHandler
how can i do reparenting of above code