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
Daniel SassoneDaniel Sassone 

Automatically create a Contact when creating a User.

Hello!
I am very new to both Salesforce and Apex, so go easy on me!

I am trying to create an Apex trigger, which creates a Contact record and automatically populates some of the fields, when someone creates a User with '@companyname.com' in the email address.

I was able to do this with Process Builder somewhat, however the only field I can't seem to populate that way, is the Account Name.  So I'm attempting to do it in Apex, though my programming knowledge is limited to Python and HTML.  Here is the code I was able to piece together, which unfortunately is still very much a rough draft.

trigger NewContactOnUser on User (after insert) {
    List<Contact> Contact_List = new List<Contact>();
    for (User New_User: trigger.new){
       
        //Conditional statement to check if new contact has @companyname.com in email field
        Contact contact_variable = new_contact();
        String new_contact_string = contact_variable.emailvalue__c;
        if (new_contact_string.contains('@companyname.com')){
  
         Contact New_Contact = new.Contact();
         New_Contact.FirstName = New_User.FirstName;
         New_Contact.LastName = New_User.LastName;
         New_Contact.Email = New_User.Email;
                contacts.add(New_Contact);
        }
    }
    insert Contact_List;
}

So I suppose my question is, how would I eventually get it to populate the Account Name field?  I did '(after insert)' because I think I need the ID for the user after it is created to then get the account name.

 

Thanks for any and all help!

Ted TkTed Tk
Contact contact_variable = new_contact();
 String new_contact_string = contact_variable.emailvalue__c;
  if (new_contact_string.contains('@companyname.com')){
This should be changed to 
for (User New_User: trigger.new){
  if (New_User.email.contains('@companyname.com')){ 
    Contact contact_variable = new_contact();

You are trying the get the email from the contact which is not created. I understand that you want to compare the email of the user and if that contains '@companyname.com' then you want to create a contact. Is that correct?
Daniel SassoneDaniel Sassone

Thanks for the reply!

Yes, the idea is it only creates a contact if the user created has an email that contains '@companyname.com' (mailto:'@companyname.com') in it.
Then, I need to assign a specific value to the Account Name field in the Contact.  I'm not sure how to do that though, since Account Name does not exist as a field in Users.

Eric Goldman 21Eric Goldman 21
I first came to this thread thinking it would explain how to create a Salesforce Contact when a new user signs up for my app. I found this tutorial helpful: https://docs.sequin.io/integrations/salesforce/playbooks/new-user-new-contact