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
nilesh walkenilesh walke 

Write a trigger to sync Mobile, Fax and Phone field between user and contact record using trigger . if no user exists for Contact record, then do nothing. Note – Create Lookup User field on Contac

 public static void syncfields(list<contact> con1){
    map<String,contact> contacts=new map<String,contact>();
    list<user> userlist=new list<user>();
    for(contact con:con1)
    {
        if(con.phone!=null)
        {
            contacts.put(con.phone,con);
        }
    }
    list<user> users=[SELECT mobilePhone,fax,phone from user where phone IN:contacts.KeySet()];

    for(user uc:users)
    {
        contact con=contacts.get(uc.phone);
        uc.phone=con.Phone;
        uc.fax= con.Fax  ;
        uc.mobilePhone= con.MobilePhone;
        userlist.add(uc);
            }
    if(userlist.size()>0){
        update userlist;
    }   
    }
working fine but , that last conditions are not matching  showing errors
Best Answer chosen by nilesh walke
Suraj Tripathi 47Suraj Tripathi 47
Hi nilesh,
You can take reference from this below code.
public static void syncfields(list<contact> con1){
        map<id,contact> contacts=new map<id,contact>();
        list<user> userlist=new list<user>();
        for(contact con:con1)
        {
            if(con.User_Id__c!=null){
                contacts.put(con.User_Id__c,con);//create lookup field inside the contact which look up with user...
            }
        }
        list<user> users=[SELECT mobilePhone,fax,phone from user where id IN:contacts.KeySet()];
        
        for(user uc:users)
        { 
            if(contacts.get(uc.id).phone!=null){
                uc.phone=contacts.get(uc.id).phone;
            }
            if(contacts.get(uc.id).Fax!=null){
                uc.fax= contacts.get(uc.id).Fax  ;
            }
            if(contacts.get(uc.id).MobilePhone!=null){
                uc.mobilePhone= contacts.get(uc.id).MobilePhone;
            }
            userlist.add(uc);
        }
        if(userlist.size()>0){
            update userlist;
        }   
    }

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer.