You need to sign in to do that
Don't have an account?
adi 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.
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.
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);
}
【Step 1】modify phone number of accout,and Save
【Step 2】The result is ok,phone number of contacts were modified automaticaly.
【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})