ddddd123aabb1212
I think the answer I wrote to Challenge with unit tests, mock callouts and running as a user provides the details you're looking for on how to create a community user in an Apex Unit Test for a Community. The subject title of the question is quite misleading compared to the content of the post. Creating a community user requires an owner which may be different than the RunAs user of the test since that user will own the Community User. The Communities User Profile needs to be specified when creating the Community User, but that user has no Role
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 con1 new Contact(LastName 'testCon',ownerIduserinfo.getuserid(),AccountId accountNewInsert.Id,Electrical_Licence_Number__cstring.valueof(55874564),email'test1211@tgd.com');insert con1;Contact contactNewInsert new Contact(LastName 'testCon',ownerIduserinfo.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, contactIdcontactNewInsert.id, TimeZoneSidKey'America/Los_Angeles', UserName'standarduser@testorg.com', isActive true);insert usr;
·OTHER ANSWER:
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 con1 new Contact(LastName 'testCon',ownerIduserinfo.getuserid(),AccountId accountNewInsert.Id,Electrical_Licence_Number__cstring.valueof(55874564),email'test1211@tgd.com');insert con1;Contact contactNewInsert new Contact(LastName 'testCon',ownerIduserinfo.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, contactIdcontactNewInsert.id, TimeZoneSidKey'America/Los_Angeles', UserName'standarduser@testorg.com', isActive true);insert usr;
Founded in 1988, Beauty is committed to skin care management,
providing customized skin services and brand products.
Provide unique experiences for everyone involved with a brand.
Do you have questions?
+86-020-22139325
Address: 5AF Whole Floor, 6F605 Room, 30F Whole Floor, xingguang Yingjing Building, No. 119 Shuiyin Road, Yuexiu District, Guangzhou.