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
Zafer KahramanZafer Kahraman 

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
    

 

CharuDuttCharuDutt
Hii Zafer
Try Below Code I've Modified The Code
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;
}
Please Mark It As Best Answer If It Helps
Thank You!
Zafer KahramanZafer Kahraman

Before trigger or after trigger give me the same error.

I think the problem is MIXED_DML_OPERATION. 
 

mukesh guptamukesh gupta
Hi Zafer,

Please follow below process to avoide MIXED DML
public class UserWithContact{
    public static void insertUser() {
        Profile pro = [SELECT Id FROM Profile WHERE Name='Standard User'];
        UserRole r = [SELECT Id FROM UserRole WHERE Name='Blogger'];
        User usr = new User(alias = 'dy', email='dineshyadav@forcepective.com', 
            emailencodingkey='UTF-8', lastname='Yadav', 
            languagelocalekey='en_US', 
            localesidkey='en_US', profileid = pro.Id, userroleid = r.Id,
            timezonesidkey='America/Los_Angeles', 
            username='dinesh.yadav@forcepective.com');
        insert usr;
        Handler.insertContact(usr.id); 
    }
}
 
public class Handler {
    @future
    public static void insertLead(String usrId) {
         User u = [Select FirstName,LastName FROM user where Id =: usrId]
         Contact c = new Contact(); 
            c.FirstName = u.FirstName; 
            c.LastName = u.LastName; 
            insert c;
       }

}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Zafer,

I have tested below code in my dev org. Itsw orking for me without an error.
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;
}

Thanks!!