• Venu Gopal
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Apex Trigger on Lead object, 2 Fileds Phone and Mobile Phone
if I make changes or updates/ modify any thing on phone same changes
needs to be happened to Mobile Phone as well?
Please Assist..
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;
        }
    }
}
  • September 22, 2021
  • Like
  • 0