Activity Forums Salesforce Questions and Answers Is it possible to create community user through Salesforce Apex?

  • Ankit

    Member
    March 28, 2018 at 11:04 am

    Is it possible to create community user through apex?

  • Adarsh

    Member
    March 28, 2018 at 11:05 am

    Hi Ankit,

    Yes, definitely it is possible through the apex.

  • Ankit

    Member
    March 28, 2018 at 11:07 am

    can we have a code please?

  • Adarsh

    Member
    March 28, 2018 at 11:09 am

    Yes afcouse,

    if (contact.Account.Owner.Role != null) {
    // Do your stuff u.firstName = '';

    User u = new User();
    u.lastName = 'Lstname';
    u.ContactId = contactLookup[0].Id;
    u.Username = 'username';
    u.Email = 'email';
    u.CommunityNickname = 'nickname';
    u.Alias = '';
    u.TimeZoneSidKey = 'America/Phoenix'; // Required
    u.LocaleSidKey = 'en_US'; // Required
    u.EmailEncodingKey = 'ISO-8859-1'; // Required
    u.LanguageLocaleKey = 'en_US'; // Required
    }

    Hope it helps 🙂

  • Archit

    Member
    March 28, 2018 at 2:52 pm

    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;

  • Neha

    Member
    March 29, 2018 at 6:26 am

    Is there is a way to insert unique Nickname user at every time when the user is inserted into my code without using timestamp function?

  • Adarsh

    Member
    March 29, 2018 at 6:51 am

    Hi Neha,

    Yes, you may use this method to accomplish it.

    public List<string> generateAliasAndNickName(string strToBeGenerated){
    if(strToBeGenerated !='' && strToBeGenerated.contains('.')){
    String[] arrSplitText = strToBeGenerated.split('\\.');
    return arrSplitText;
    }else if(strToBeGenerated !='' && !strToBeGenerated.contains('.')){
    return new List<String>{strToBeGenerated};
    }else{
    return null;
    }
    }
    public boolean validateIsDuplicateNickName(String strNickname){
    List<User> lstExistingUser = new List<User>();
    if(strNickname != null && strNickname != ''){
    lstExistingUser = [select id,communityNickname from User where communityNickname =:strNickname];
    }
    if(lstExistingUser != null && !lstExistingUser.isEmpty()){
    return true;
    }
    else
    return false;
    }

    and whenever it found a duplicate, concatenate the Nickname string with +1 increment.

    hope it helps 🙂

  • PRANAV

    Member
    March 29, 2018 at 8:37 am

    Hi Neha,

    You can also post the separate question for same for faster response.

  • Neha

    Member
    March 29, 2018 at 10:46 am

    Thanks for your suggestion. I will do that.

  • Parul

    Member
    September 20, 2018 at 6:31 pm

    Hi,

    It is possible to create community user from apex by putting contactid while creating user.

    following us pseudo for the same

    User u = new user();
    u.LastName = ‘Test Code’;
    u.Email = ‘[email protected]’;

    u.ContactId = contact.Id;
    u.Alias = ‘Tcode’;
    u.Username = ‘[email protected]’;
    u.CommunityNickname = ‘test123’;
    u.LocaleSidKey = ‘en_US’;
    u.TimeZoneSidKey = ‘GMT’;
    u.profileId = ’00e28000001ZWE4′;
    u.LanguageLocaleKey = ‘en_US’;
    u.EmailEncodingKey = ‘UTF-8’;
    insert u;

  • shariq

    Member
    September 20, 2018 at 10:21 pm

    Hi,

    Yes, you can create community  user through apex, just remember community user has extra field contactId which is required.

    Hope this helps.

Viewing 1 - 11 of 11 posts

Log In to reply.





Popular Salesforce Blogs