function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
adi salesforceadi salesforce 

Trigger to update contact phone number

I wrote account handler class which will update contact phone number,other street, description by changing the account phone number,billing street, description.

public class Accounttriggerhandlert {
public static void afterupdate(map<id,account>newmap){
        list<contact>cons=new list<contact>();
        map<id,account>accsmap=new map<id,account>();
        list<account>acclist=[select id,phone,billingstreet,description, (select id,phone,description,otherstreet from contacts)
                          from account where id in:accsmap.keyset()];
        for(account a:acclist){
            for(contact c:a.contacts){
                c.phone=a.Phone;
                c.description=a.description;
                c.otherstreet=a.billingstreet;
                cons.add(c);
            }
        }
        update cons;
    }
}



trigger:

trigger acctottrigger on Account (after update) {
accounttriggerhandlert.afterupdate(trigger.newmap);
}


could you correct this code.
Jack Yu@TokyoJack Yu@Tokyo
Hi, i modify your code.

public class Accounttriggerhandlert {
public static void afterupdate(List<account> accountList){
        list<contact>cons=new list<contact>();
        //map<id,account>accsmap=new map<id,account>();
        list<account>acclist=[select id,phone,billingstreet,description, (select id,phone,description,otherstreet from contacts)
                          from account where id in: accountList];
        for(account a:acclist){
            for(contact c:a.contacts){
                c.phone=a.Phone;
                c.description=a.description;
                c.otherstreet=a.billingstreet;
                cons.add(c);
            }
        }
        
        system.debug('cons--->'+cons);
        update cons;
    }
}


trigger:
​trigger acctottrigger on Account (after update) {
    accounttriggerhandlert.afterupdate(trigger.new);
}


User-added image

【Step 1】modify phone number of accout,and Save
User-added image


【Step 2】The result is ok,phone number of contacts were modified automaticaly.​

User-added image


【Log output is below.】
cons--->
(Contact:{AccountId=0017F00000DR5xjQAD, Id=0037F000007iPh7QAE, Phone=(650) 867-1234, Description=Genomics company engaged in mapping and sequencing of the human genome and developing gene-based drugs, OtherStreet=345 Shoreline Park
Mountain View, CA 94043
USA}, 

Contact:{AccountId=0017F00000DR5xjQAD, Id=0037F00000XnOvnQAF, Phone=(650) 867-1234, Description=Genomics company engaged in mapping and sequencing of the human genome and developing gene-based drugs, OtherStreet=345 Shoreline Park
Mountain View, CA 94043
USA})