Activity Forums Salesforce® Discussions How to create a User through Salesforce Apex?

  • PRANAV

    Member
    March 28, 2018 at 10:30 am

    Hi Ankit,

    The below code will help you, but before running the code please check the required field on User Object, so that you need to include those also.

    Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator' limit 1];
    list<User> usr = new list<User>();
    User u = new User(Alias = 'st', Email='[email protected]',
    EmailEncodingKey='UTF-8', LastName='ABC', LanguageLocaleKey='en_US',
    LocaleSidKey='en_US', ProfileId = p.Id , CustomUserName__c = 'Test name',
    TimeZoneSidKey='America/Los_Angeles', Username='[email protected]',
    CommunityNickname = 'Test');
    usr.add(u);
    insert u;

    Hope this helps you.

  • Adarsh

    Member
    March 28, 2018 at 10:40 am

    Hi ankit,

    Profile p = [SELECT Id FROM Profile WHERE Name=’System Administrator’ limit 1];

    User u = new user();
    u.LastName = 'Test Code Welcome';
    u.Email = '[email protected]';
    u.Alias = 'Tcode';
    u.Username = '[email protected]';
    u.CommunityNickname = 'test1211';
    u.LocaleSidKey = 'en_US';
    u.TimeZoneSidKey = 'GMT';
    u.ProfileID = p.id;
    u.LanguageLocaleKey = 'en_US';
    u.EmailEncodingKey = 'UTF-8';
    insert u;

  • Archit

    Member
    March 29, 2018 at 4:55 am

    User u = new user();
    u.LastName = ‘Test Code’;
    u.Email = ‘[email protected]’;
    u.Alias = ‘Tcode’;
    u.Username = ‘[email protected]’;
    u.CommunityNickname = ‘test12’;
    u.LocaleSidKey = ‘en_US’;
    u.TimeZoneSidKey = ‘GMT’;
    u.profileId = ’00e28000001Xsf3′;
    u.LanguageLocaleKey = ‘en_US’;
    u.EmailEncodingKey = ‘UTF-8’;
    insert u;

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos