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
Kiran456Kiran456 

How to create user through apex.

how to create user through apex code.

hitesh90hitesh90

Hi Kiran,

 

we can create user through apex. here is the code.

 

User u = new user();
u.LastName = 'Test Code';
u.Email = 'test@test.com';
u.Alias = 'Tcode';
u.Username = 'test1234444@test.com';
u.CommunityNickname = 'test12';
u.LocaleSidKey = 'en_US';
u.TimeZoneSidKey = 'GMT';
u.ProfileID = '00e90000000oyi5';
u.LanguageLocaleKey = 'en_US';
u.EmailEncodingKey = 'UTF-8';
insert u;

 

Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

 

Thanks,

Hitesh Patel

Jason Liveston 8Jason Liveston 8
Below is an example of creating a user.  
 
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
        
     	User usr = new User(LastName = 'LIVESTON',
                           FirstName='JASON',
                           Alias = 'jliv',
                           Email = 'jason.liveston@asdf.com',
                           Username = 'jason.liveston@asdf.com',
                           ProfileId = profileId.id,
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                           LocaleSidKey = 'en_US'
                           );

 
Gulam Sabir Khan 5Gulam Sabir Khan 5
Hi Kiran,

You can try below, Hope it will work

Profile profId = [select Id from Profile where name=:'Standard User' limit 1];
String orgId = UserInfo.getOrganizationId();
String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
String uniqueName = orgId + dateString + randomInt;
if(uniqueName.length()>51)
{
     uniqueName=uniqueName.substring(0,51);
 }
 User tuser = new User( firstname = 'Test',
  lastName = 'Kiran',
 email = uniqueName + '@test' + orgId + '.org',
 Username = uniqueName + '@test' + orgId + '.org',
 EmailEncodingKey = 'ISO-8859-1',
 Alias = uniqueName.substring(18, 23),
 TimeZoneSidKey = 'America/Los_Angeles',
  LocaleSidKey = 'en_US',
  LanguageLocaleKey = 'en_US',
  ProfileId = profId.Id);
   insert tuser;

Thanks,
Regards,
Sabir
Ajay K DubediAjay K Dubedi
Hi Kiran,

Below Sample code can fulfill your requirements. Hope this will work for you.

      /* Fill all mandatory fields */
       user.Username = 'test@samp.com';
       user.Email = 'test@samp.com';
       user.Alias = 'tsamp';
       user.UserRoleId = 'user_role_id_goes_here';
       user.ProfileId = 'user_profile_id_goes_here';
       user.Employment_Start_Date__c = '10/01/2018';
       user.IsActive = false;  // here we are creating inactive user account
       try
      {
           insert user;  // insert the user record
       }
     catch(Exception e)
     {
          System.debug(e);
      
     }

Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi
ATUL GUPTA 94ATUL GUPTA 94
Hello User is Created but  I did not recieved Email.