• #26818
    Salesforce | Ankit Ankit #951
    Forcetalks

    Is it possible to create community user through Salesforce Apex?

    Is it possible to create community user through apex?

     

    #26820
    Salesforce | Adarsh Adarsh #958
    Forcetalks

    Hi Ankit,

    Yes, definitely it is possible through the apex.

    #26822
    Salesforce | Ankit Ankit #951
    Forcetalks

    can we have a code please?

    #26824
    Salesforce | Adarsh Adarsh #958
    Forcetalks

    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 🙂

    #26853
    Salesforce | Archit Archit #957
    Forcetalks

    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;

    #26909
    Salesforce | Neha Neha #956
    Forcetalks

    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?

    #26919
    Salesforce | Adarsh Adarsh #958
    Forcetalks

    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 🙂

    #26933
    Salesforce | PRANAV PRANAV #954
    Forcetalks

    Hi Neha,

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

    #26989
    Salesforce | Neha Neha #956
    Forcetalks

    Thanks for your suggestion. I will do that.

    #35822
    Salesforce | Parul Parul #953
    Forcetalks

    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;

    #35920
    Salesforce | shariq shariq #959
    Forcetalks

    Hi,

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

    Hope this helps.

Viewing 11 posts - 1 through 11 (of 11 total)

Please to reply to this topic.

Popular Salesforce Blogs

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2020 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?