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
Siva SakthiSiva Sakthi 

How to create Standard Platform User via apex coding

Hi,

We can create the users via dataloader and normal procedure via Administer-Manage Users- Users - New users. ' How to create the platform users via apex ' coding. Please share any example coding here.  
SantoshChitalkarSantoshChitalkar
Hi Maheshwar,

Refer following code - 
 
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing123', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduserTestUser@testorg.com');

        insert u;

Pass the Username, EmailId and Lastname as per your need.
Mark this question as solved and choose the best answer if this solves your problem.

Regards , 
Santosh Chitalkar
 
Siva SakthiSiva Sakthi

Thanks Santosh,

The below is correct or worng , I miss anything .create the users via getting all information from the vf page fields click save/ create dynamically . 

public string createuser (User user) {

User newuser = new User();
newuser.Alias = user.Alias; 
newuser.Email=user.Email ;
newuser .EmailEncodingKey=user.EmailEncodingKey ;
newuser .LastName=user.LastName  ;
newuser .LanguageLocaleKey=user.LanguageLocaleKey; 
newuser .LocaleSidKey=user.LocaleSidKey; 
newuser .ProfileId = user.Profile; 
newuser .TimeZoneSidKey=user.TimeZoneSidKey;
newuser .UserName=user.UserName;          

        if(user.Id != null || String.isEmpty(user.Id)) {
            System.debug('Creating New User');
            insert newuser;
            return 'new User created';
        }
        else {
            System.debug('Updating existing user');
            newuser.Id = user.Id;
            update newuser;
            return 'user updated';
        }
 }


Advance Thanks
Maheshwar
SantoshChitalkarSantoshChitalkar
Looks fine to me
Siva SakthiSiva Sakthi
Thanks, 

I tried the above code what you post its working fine. But email (with change password link)  not triggered to the users. How to get this via trigger or workflow ( with sample code for trigger ) please guide me to solve this.

Maheshwar,
Siva SakthiSiva Sakthi
Hi,

Using this below code able to create the user successfully.' I want to get the newly inserted user Id Immediately' .Once again we put the query to get a user id or Is any way to get the user id. how to get this . Using Userinfo.getUserId() we can get current loged in user. Please guide me to achive this .
Indira PadhiIndira Padhi
To trigger the emal set the dmloption for triggering email. 


public class CreateUserClass {

    public static string CreateUser (User u) {
    
    User newu = new User();
    newu.Alias = u.Alias; 
    newu.Email=u.Email ;
    newu.EmailEncodingKey=u.EmailEncodingKey ;
    newu.LastName=u.LastName  ;
    newu.LanguageLocaleKey=u.LanguageLocaleKey; 
    newu.LocaleSidKey=u.LocaleSidKey; 
    newu.ProfileId = u.Profileid; 
    newu.TimeZoneSidKey=u.TimeZoneSidKey;
    newu.UserName=u.username;  
    //newu.UserType= u.UserType;       
newu.IsActive=true; 
            if(u.Id != null || String.isEmpty(u.Id)) {
                System.debug('Creating New User');
                List<User> usersl = new List<User>(); 
               usersl.add(newu);
                Database.DMLOptions dlo = new Database.DMLOptions();
                dlo.EmailHeader.triggerUserEmail = true; 
                database.insert(usersl, dlo);

                
               // insert newu;
                return usersl[0].id;
            }
            else {
                System.debug('Updating existing user');
                newu.Id = u.Id;
                update newu;
                return 'user updated';
            }
     }
    
}

Select this answer if it helps u.