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
UrvikUrvik 

Create a Contact Record upon User Record Creation

I have been trying to create a contact record once a user record of a specific profile has been created. I am able to save the trigger but cannot create a user record. I have posted a code below. 
trigger createContact on User (before insert) {     
List<Contact> ur = new List<Contact>();
    for (User usr: Trigger.New)
    //IF(Usr.UserRoleId == '00EK0000000KRUR'){
          ur.add (new Contact(
                     FirstName = Usr.FirstName,
                     LastName = Usr.LastName));
                  //  }   
         
   
   insert ur;
 }
Chris ShadeChris Shade
Hi Urvik,

I think this should work:
 
trigger createContact on User (after insert) {    
    List<Contact> ur = new List<Contact>();
    for (User usr: Trigger.New){
              ur.add (new Contact(
                         FirstName = usr.FirstName,
                         LastName = usr.LastName));
    }
    insert ur; 
}

Thanks,
Chris

Hit the Like button if this was helpful.
 
Geoffrey J FlynnGeoffrey J Flynn
This one works, provided you have both a first and last name when creating a user record
You are missing the { after the For statement
trigger NewContactOnUser on User (before insert) {
    List<Contact> contacts = new List<Contact>();
    for (User u: trigger.new){
        Contact c = new Contact();
        c.FirstName = u.FirstName;
        c.LastName = u.LastName;
        contacts.add(c);       
    }
    insert contacts;
}
Chris's after insert change is incorrect in my opinion, this should be a before trigger.
Layering on a profile restriction should be easy, but I would stay away from hardcoding the ID where possible

 
Chris ShadeChris Shade
Hey Geoffrey,

I'm pretty new to apex.  Why is a before insert better than an after insert?

Thanks,
Chris
Geoffrey J FlynnGeoffrey J Flynn
Hi Chris,
You use an after insert if you need the ID from the record you are inserting.
So for example if you were automatically creating an Opportunity when a user creates an account, you would do an after insert trigger because you would need the AccountID in order to create the Opportunity.  It is After because you don't know the ID until it has been inserted.  
Glyn Anderson 3Glyn Anderson 3
Chris,  I disagree with Geoffrey.  I would put this code in an after trigger.  I reserve before triggers for logic that operates on the records that are in the trigger, and after triggers for logic that operates on related records.  This way, all DML is done in after triggers and never in before triggers.  DML can begin a chain reaction of triggers and more DML and more triggers.  His point is valid that the before insert trigger won't contain the record IDs, and in your use case, you don't need the record IDs; but nevertheless, since you are modifying a different object (Contact - not User), I would put this logic into the after trigger.  (Just my two cents...)
Glyn Anderson 3Glyn Anderson 3
Urvik,  Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  If you solved it yourself another way, please post your solution.  Thanks!