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

Trigger to Create Contact Record
I am trying to create a contact record upon user record creation. It should only create a contact record if certain user role are selected. I have the following trigger which works fine if I remove IF statement for ROLE...could someone please help? Again, I have been trying to create a contact record upon creating user record only when certain ROLE are set for the user...Thanks..
trigger CreateContact on User (after insert) { List<Contact> ur = new List<Contact>(); for (User usr: Trigger.New){ if(usr.UserRole.Name=='role name'|| usr.UserRole.Name=='role name') ur.add (new Contact( FirstName = usr.FirstName, LastName = usr.LastName)); } if(!ur.isEmpty()) insert ur; }
Records in Trigger.New will not have access to UserRole.Name field as the field is further level down in relationship....You'll need to query for UserRole ids and then match that by looping the user records..see the code below.
Thanks,
Mohammed
I can't think of any specific reason for this not working unless the query is not returning any matching records...Let me add few debug lines and you can setup debug on your user and see whats happening. Try using the code below with debug lines..
Hope it helps,
Thanks,
Mohammed