You need to sign in to do that
Don't have an account?

Update phone field on contact from account phone field if it is blank for all contacts under that account
Hi, Im new to triggers and apex and I need to update phone field (if blank) on contacts from the account field for existing records:
Here is my try , but Im sure Im missing some elements. Any help is appreciated:
trigger updatephonefield on Contact (after update, after insert ) {
for (Contact cp : Trigger.new) {
if ( cp.Phone == null ) {
cp.Phone == Account.Phone
}
}
Here is my try , but Im sure Im missing some elements. Any help is appreciated:
trigger updatephonefield on Contact (after update, after insert ) {
for (Contact cp : Trigger.new) {
if ( cp.Phone == null ) {
cp.Phone == Account.Phone
}
}
All Answers
you can achieve this through workflow with field update || Process Builder..
Workflow Criteria:
Contact phone is null and evrytime record is create and it's edited
Action: Field Update:
Conatct Phone = Account.Phone
Thank you,
Anil
I dont understand why its not working....I have if contact.phone is null and returns True -> then action is field Contact.phone gets reference from Account.phone
Thanks!
Set<id> setaccid = new Set<id>();
List<Contact> newclist = new List<Contact>();
for (Account acc : Trigger.new) {
if ( acc.Phone != null && acc.phone != trigger.oldmap.get(acc.id).phone) {
setaccid.add(acc.id);
}
List<Contact> clist = [Select id,phone from contact where accountid IN: setaccid];
for(contact cp:clist){
if(cp.Phone==null)
cp.Phone = acc.Phone;
newclist.add(cp);
}
}
update newclist;
}