+ Start a Discussion
Nitin LedangeNitin Ledange 

need to update the Case Contact Phone field with Account Phone value whenever Account record is updated through the Account Trigger

need to update the Case Contact Phone field with Account Phone value whenever Account record is updated through the trigger.
My code : 
trigger CaseUpdate on Account (After update) {

    List<Case> toUpdateCases = new List<case>();
    for(Account a : Trigger.New){
        for(Account acc : [Select ID, Name, Phone,(Select ID, Contact.Phone, ContactId from Cases) from Account where ID=: a.Id]){
            for(Case C : acc.Cases){
                   c.Contact.Phone = acc.Phone;
                   toUpdateCases.add(c);
             }
        }
    }
    update toUpdateCases;
}

No Error but not Working.
Need help!
Pradeep SinghPradeep Singh
Hi Nitin,
You have to update contact records for this.

Refer Below code :-

Map<id,string> ConIdAccPh_Map = new Map<id,string>();
List<Contact> conList = new List<Contact>();
for(Case C : [Select ID, Contact.Phone,Account.Phone, ContactId from Cases where AccountId In:trigger.new]){
    CaseIdAccPh_Map.put(C.ContactId,C.Account.Phone);
}

if(ConIdAccPh_Map != Null){
    for(Contact con : [Select id,name,Phone from Contact where id IN:ConIdAccPh_Map.keyset()]){
        if(ConIdAccPh_Map.containskey(con.id)){
            con.Phone = ConIdAccPh_Map.get(con.id);
            conList.add(con);
        }
    }
}

If(conList.size() > 0){
    update conList ;
}