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
Sammy7Sammy7 

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
            }
}
Best Answer chosen by Sammy7
Laraib_JafriLaraib_Jafri
Yes, that is correct. A workaround for that would be to create a hidden checkbox field on Contacts and using data loader mark it to True/False for all contact records. That should "update" all contact records and trigger the process builder trigger.

All Answers

Laraib_JafriLaraib_Jafri
You can do this using Process Builder, no need to use a trigger.
Anil MalneniAnil Malneni
Hi Simran

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
Rakesh51Rakesh51
Yes you can do this using declarative feature of salesforce that is Process Builder. 
Sammy7Sammy7
I've tried doing this in process builder, but it wont show the "phone" field for contacts.....sadly Im using a professional edition of SFDC and dont have access to workflow so was thinking trigger might be easiest...I need to figure out how to reference the account.phone field in apex....
Rakesh51Rakesh51
Make sure to set Field level security to visible and editable for Phone field at least system administrator profile. You can easily do it through Process Builder
Sammy7Sammy7
processbuilder1 2ndpic

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 
Laraib_JafriLaraib_Jafri
That seems like its correct, did you remember to Activate the flow?
Sammy7Sammy7
Ok, I think I see the limitations here: it only works on new records or once records are edited. It works on these conditions, but NOT on existing records...
Laraib_JafriLaraib_Jafri
Yes, that is correct. A workaround for that would be to create a hidden checkbox field on Contacts and using data loader mark it to True/False for all contact records. That should "update" all contact records and trigger the process builder trigger.
This was selected as the best answer
Sammy7Sammy7
Thats just what I was thinking....just upload with data loader :-)
Thanks!
smriti sharan19smriti sharan19
trigger updatephonefield on account (after update ) {
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;
}