You need to sign in to do that
Don't have an account?
Automatically create a Contact when creating a new User.
Hi Everyone,
I am trying to create a new contact record when the new user is created.
This trigger code gives this error.
trigger NewContactOnUser on User (after 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;
Error is this..
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger NewContactOnUser caused an unexpected exception, contact your administrator: NewContactOnUser: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Contact, original object: User: []: Trigger.NewContactOnUser: line 9, column 1
Try Below Code I've Modified The Code Please Mark It As Best Answer If It Helps
Thank You!
Before trigger or after trigger give me the same error.
I think the problem is MIXED_DML_OPERATION.
Please follow below process to avoide MIXED DML
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
I have tested below code in my dev org. Itsw orking for me without an error.
Thanks!!