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
laxmi narayan 11laxmi narayan 11 

Create a custom object record when user create in salesforce.

Hi there,

When i created a user in salesforce, it will create a record on another object with same name. How do i write a trigger for it.

 

Khan AnasKhan Anas (Salesforce Developers) 
Hi Laxmi,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger NewRecordOnUserInsert on User (before insert) {
    List<CustomObject__c> cust = new List<CustomObject__c>();
    for (User u: trigger.new){
        CustomObject__c c = new CustomObject__c();
        c.Name = u.FirstName;
        cust.add(c);       
    }
     if(cust.size()>0){
        INSERT cust;
     }
}

Also, please refer to the below link which might help you further.

https://crmdev.blog/2017/07/05/an-employee-contact-record-for-every-salesforce-user/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
JayantJayant
You may also do it using Process Builder, Configuration before Code :).
laxmi narayan 11laxmi narayan 11
Hi Khan Anas,

I appriciate your help!

when i create a user it shows me an error " 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)".


Thanks
 
JayantJayant
If you want to go with a trigger instead of Process, move the above trigger's code to a class method and mark it as @future. Call that method from trigger, Mixed DML would be resolved.
Khan AnasKhan Anas (Salesforce Developers) 
+1 Jayant

DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions. 

The solution for this issue is to use a future method to perform the DML operations.

Try this:

Class:
public class NewRecordHandler {
    
    @future 
    public static void createNewRecords (List<User> users)  {
        List<CustomObject__c> cust = new List<CustomObject__c>();
        for (User u: users){
            CustomObject__c c = new CustomObject__c();
            c.Name = u.FirstName;
            cust.add(c);       
        }
        if(cust.size()>0){
            INSERT cust;
        }
    }
}

Trigger:
trigger NewRecordOnUserInsert on User (before insert) {
    
    NewRecordHandler.createNewRecords(Trigger.new);
}

Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi laxmi,
Try this code:
trigger:
trigger CreateRecordBasedOnUser on User (after insert) {
    if(trigger.isInsert && trigger.IsAftre) {
        CreateRecord.insertRecord(trigger.new);
    }
}
trigger handler:
public class CreateRecord {
    
    public static void insertRecord(List<User> userList) {
        try {
            if(userList.size() > 0) {
                CustomObject__c cObj = new CustomObject__c ();
                // here put all required fields.
                insert cObj;
            }
        }
        catch(Exception ex){
            system.debug('Exception---ofLine--->'+ex.getLineNumber());
            system.debug('Exception---Message--->'+ex.getMessage());
            
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi