Activity Forums Salesforce® Discussions Is it possible to create community user through Salesforce Apex?

  • Adarsh

    Member
    March 28, 2018 at 11:05 AM

    Hi Ankit,

    Yes, definitely it is possible through the apex.

  • [adinserter block='9']
  • 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 = 'test@test.com';
    u.Alias = 'Tcode';
    u.Username = 'test1234444@test.com';
    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 = ‘test@test.com’;

    u.ContactId = contact.Id;
    u.Alias = ‘Tcode’;
    u.Username = ‘test11@test.com’;
    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.

Log In to reply.

Popular Salesforce Blogs

Skip to toolbar