INTERNAL USER
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
List<User> uu = new List<User>();
while (uu.size() < 5) {
Blob b = Crypto.GenerateAESKey(128);
String h = EncodingUtil.ConvertTohex(b);
String uid = h.SubString(0,8);
User u = new User(Alias = uid, Email= uid + '@testdomain.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/New_York', UserName= uid + '@testdomain.com');
uu.add(u);
}
insert uu;
EXTERNAL USER OR COMMUNITY USER
Account a = new Account(Name = 'ABC Corp'); // add more fields based on your org validation
insert a;
Contact c = New Contact(LastName = 'Test', AccountID = a.id);
insert c;
// system.debug('Contact = ' + c.Id);
Profile p = [SELECT Id FROM Profile WHERE Name = 'Customer Community user' LIMIT 1];
user u = New User(
UserName = 'test_' + math.random() + '@test.com',
FirstName = 'Test-First',
LastName = 'Test-Last',
Alias = 'test',
email = 'test' + math.random() + '@test.com',
CommunityNickName = string.valueOf(math.random()).substring(0,6),
ProfileID = p.id,
TimeZoneSidKey = 'America/New_York',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
ContactId = c.Id
);
insert u;