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
Mike AgresMike Agres 

Trigger on User to Update that User's Contacts

Use Case: if certain field values on a User record changes, I want that change to appear in corresponding fields on all the Contact records that this user owns.

I figure I need to create this trigger on the User object, but how do I call that User's Contact records to update their values?
Chandrashekhar GoudChandrashekhar Goud
Hi
Apex Class for both update and create:
global class usertocontact {
    @future
     /* Code for Update a contact using User */
    public static void usertocon(Set<Id> userIds) {
        List<User> user = [select Id, firstname, lastname, email from user where Id IN :userIds];
        List<contact> cons = new List<contact>();
        for (User usr : User) {
            
            contact c = new contact ();
            c.FirstName = usr.FirstName;
            c.LastName = usr.LastName;
            cons.add(c);
        }
        update cons;
        
    } 
    /* Code for create a contact using User */
    public static void usertocon() {
        List<Contact> contacts = new List<Contact>();
        List<User> user = [select Id, firstname, lastname, email from user ];
        for (User u: User){
            Contact c = new Contact();
            c.FirstName = u.FirstName;
            c.LastName = u.LastName;
            contacts.add(c);       
        }
        insert contacts;
    } 
}
Trigger
/*Trigger for Create */
trigger NewContactOnUser on User (before insert) {
 usertocontact.usertocon();
}


/*Trigger for update*/
trigger usertoContact on User (after insert) {
  usertocontact.usertocon(Trigger.newMap.keySet());

}


 
Hetvi SalesforceHetvi Salesforce
Hello @ Mike Agres, I have the Same Task... Did You get any Solution?