You need to sign in to do that
Don't have an account?

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; }
I think this should work:
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.
You are missing the { after the For statement 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
I'm pretty new to apex. Why is a before insert better than an after insert?
Thanks,
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.