You need to sign in to do that
Don't have an account?
KMN
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;
}
}
}
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;
}
}
}
You can use this code, it is working according your requirement :
Trigger-->
Handler-->
If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi
In your code you are also using loop inside the loop..
just that thing i want to avoid so how can it be possible
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