Activity › Forums › Salesforce® Discussions › Is it possible to create community user through Salesforce Apex?
Tagged: Community, Community User, Create User, Salesforce Apex, Salesforce Apex Class, Salesforce Apex Code, Salesforce Apex Controller, Salesforce Community
-
Is it possible to create community user through Salesforce Apex?
Posted by Ankit on March 28, 2018 at 11:04 AMIs it possible to create community user through apex?
shariq replied 7 years, 7 months ago 7 Members · 10 Replies -
10 Replies
- [adinserter block='9']
-
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 🙂
-
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; -
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?
-
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 🙂
-
Hi Neha,
You can also post the separate question for same for faster response.
-
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; -
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.