[SalesForce] Update Account field phone then same phone number will set on related contact

I have create Trigger on Account Object and Requirment is whenever i update Account Object Phone field then same phone number will apply on Related Contact(child contact of Account). this code will not working

—-Trigger ——

trigger Contact_Phone_upd_Trigger on Account (After Update) {
    if(Trigger.isUpdate){
        Contact_Phone_Handler.perform_AfterUpdate(Trigger.New, Trigger.OldMap);
    }
}

—–Handler Class —-

public class Contact_Phone_Handler{
    public static void perform_AfterUpdate(List<Account> newList, Map<Id,Account>oldMapAcc){
        Map<Id,Account> AccID = new Map<Id,Account>();
        for(Account acc : newList){
            Account oldAcc=oldMapAcc.get(acc.id);

            if(acc.phone != oldAcc.phone){
                AccId.put(acc.id,acc);
            }
        }


        List<Contact> Con = [SELECT Id, phone FROM Contact where ID IN : AccId.KeySet()];

            for(Contact c : con){
                Account acc=AccId.get(c.accountId);
                c.phone = acc.phone;
            }

        if(Con.size()>0 && Con != null){
            update Con;
        }


    }
}

Best Answer

Your query of Contact where clause uses AccountId instead of id

public class Contact_Phone_Handler{

    public static void perform_AfterUpdate(List<Account>newList, Map<Id,Account>oldMapAcc){

        Map<Id,Account> AccID = new Map<Id,Account>();
        for(Account acc : newList){
            Account oldAcc=oldMapAcc.get(acc.id);

            if(acc.phone != oldAcc.phone){
                AccId.put(acc.id,acc);

            }
        }

        List<Contact> Con = [SELECT Id, phone, AccountId 
                               FROM Contact where AccountId IN : AccId.KeySet()];

        for(Contact c : Con){
                Account acc=AccId.get(c.accountId);
                c.phone = acc.phone;

        }

        if(Con.size()>0 && Con != null){
            update Con;
        }
    }
}