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
nandan patranandan patra 

User Creation

Hii All,
How can I create an user using apex code,
thanks in advance.
bob_buzzardbob_buzzard
Here's a very simple example - note that you should avoid hardcoding values :
 
Profile prof=[select id from Profile 
                     where name='System Administrator'];

User user = new User(Alias = 'unitTest', Email='testuser@testorg.com', 
                     EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                     LocaleSidKey='en_GB', ProfileId = prof.Id, 
                     TimeZoneSidKey='Europe/London', UserName='unit.test@unit.test');

insert user;

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
http://salesforce.stackexchange.com/questions/3967/creating-a-user-with-dml
http://murthyvvr.blogspot.in/2013/11/creating-salesforce-users-from-apex.html
http://codedevstuff.blogspot.in/2014/07/create-salesforce-user-in-apex-with-all.html
 
Profile pf = [SELECT Id FROM profile 
                          WHERE name='Custom Portal User Profile' limit 1];   
            //Create user 
            User mockUser = new User(contactId=objectContact.Id, 
                            username=objectContact.Email, 
                            firstname=objectContact.FirstName,
                            lastname=objectContact.LastName, 
                            email=objectContact.Email,
                            communityNickname = objectContact.LastName + '_'+Math.random(),
                            alias = string.valueof(objectContact.FirstName.substring(0,1) + 
                                    objectContact.LastName.substring(0,1) + Math.random() ), 
                            profileid = pf.Id, emailencodingkey='UTF-8',
                            languagelocalekey='en_US', 
                            localesidkey='en_US', 
                            timezonesidkey='America/Los_Angeles');                  
            insert mockUser;


 
Prad NethasPrad Nethas
Are you using this for test class? please let me know.

Regards
Prad
nandan patranandan patra
I am confused..when I am trying to execute it gives an error @ Prad Nethas
bob_buzzardbob_buzzard
Any chance you could post the error?
nandan patranandan patra
Line: 9, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_USERNAME, Duplicate Username.<br>The username already exists in this or another Salesforce organization. Usernames must be unique across all Salesforce organizations. To resolve, use a different username (it doesn't need to match the user's email address). : [Username]
bob_buzzardbob_buzzard
So the error is pretty clear - there is already a user with that name in Salesforce (not necessarily your Salesforce) and usernames have to be unique. You will therefore have to choose a different username for your new user.
bob_buzzardbob_buzzard
If you are using my code, that will fail as I just created a user to test it :) 
nandan patranandan patra
Dear Bob, I am very beginner in apex. can u send me the code by changeing the user name..? please
bob_buzzardbob_buzzard
Just change the username value to something other than unit.test@unit.test. If you are struggling with that you might want to go back to basics with Apex via Trailhead:

https://developer.salesforce.com/trailhead/module/apex_database
nandan patranandan patra
 yaaa...its succesfully running...I need some more help..like how to see it and how to add some feature to that user @BOB
 
Prad NethasPrad Nethas
Hi Nandan,

Make sure that every time you create user using the apex, you should give unique name as mentioned by Bob.

Regards
Prad