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
Manjunath reddy 6Manjunath reddy 6 

copy phone from contacts to related accounts phones

I have a  requirement to copy phone from contact to related account phone.but my trigger not works, can some one help me on this
trigger copy on account(after insert,after update) {

List<id> carry=New list<id>();
For(account acc:trigger.new){
carry.add(acc.id);

}
List<Contact> hold=[select id, name,phone from contact where id IN: carry];
List<account> take=[Select id,phone from account where id IN: carry];
for(account acc:take){
for(contact c:hold){
acc.phone=c.phone;
}
}
}

I tried to create a trigger on contact also as below but not works.
trigger abc on Contact (after insert,after update) {

List<id> carry=New list<id>();
For(contact c:trigger.new){
carry.add(c.id);

}
List<Contact> hold=[select id, name,phone from contact ];
List<account> take=[Select id,phone from account where id IN: carry];
for(account acc:take){
for(contact c:hold){
acc.phone=c.phone;
}
}

}
Best Answer chosen by Manjunath reddy 6
Sanpreet SainiSanpreet Saini
Hi Manju Reddy, 

Try this. 
 
trigger abc on Contact (after insert,after update) {
list <id> accid = new list<id>();
list <account> acc = new list <account>();
For(contact c:trigger.new){
accid.add(c.accountid);
}
for(account a :[select id,phone,(select phone from contacts where id in : trigger.new) from account where id in  : accid]){
for(contact c1 : a.contacts){
a.phone = c1.phone;
acc.add(a);
}}
update acc;
}

Regards, 
Sanpreet