Is it possible to assign a user access to a community when creating the same in an apex class?
Currently I am trying to create a user from an apex Test class and I am providing the user with a profile Id. How can I grant the user access to a community.
The code I have used is given below. I am creating a user and have assigned the same with a profile. But still when I use the system.runAs the user is not assigned to the community.
Account accountNewInsert = new Account(name ='Grazitti',Application_Type__c='Sole Proprietor') ;
insert accountNewInsert;
List<Contact> LewContact = new List<Contact>();
Contact con1 = new Contact(LastName ='testCon',ownerId=userinfo.getuserid(),AccountId = accountNewInsert.Id,Electrical_Licence_Number__c=string.valueof(55874564),email='test1211@tgd.com');
insert con1;
Contact contactNewInsert = new Contact(LastName ='testCon',ownerId=userinfo.getuserid(),AccountId = accountNewInsert.Id,Electrical_Licence_Number__c='554564',email='test1211@tgd.com');
insert contactNewInsert;
system.debug('Test Contact'+ contactNewInsert);
Profile p = [SELECT Id FROM Profile WHERE Name='Registered Electrical Contractor'];
User usr= new User(
Alias = 'standt',
Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8',
LastName='Testing',
LanguageLocaleKey='en_US',
LocaleSidKey='en_US',
ProfileId = p.Id,
contactId=contactNewInsert.id,
TimeZoneSidKey='America/Los_Angeles',
UserName='standarduser@testorg.com',
isActive = true
);
insert usr;