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='[email protected]');

insert con1; 

Contact contactNewInsert = new Contact(LastName ='testCon',ownerId=userinfo.getuserid(),AccountId = accountNewInsert.Id,Electrical_Licence_Number__c='554564',email='[email protected]');

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='[email protected]',
    ProfileId = p.Id,
    UserName='[email protected]',
    isActive = true
insert usr;
  • If the profile is already linked to the community you will not need to explicitly add them.
    – Dave Humm
    Apr 1, 2017 at 19:08

2 Answers 2


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.

  • I went through the other thread. I am creating a user and am assigning the user with a profile like you said. But still the user is not being assigned the community status. The reason why I say this is when I try to edit a particular object from the test class It says System.QueryException: sObject type 'AccountContactRole' is not supported @crmprogdev Jun 15, 2015 at 6:18
  • What you're describing is a permissions related issue. If you look at the Object Reference, you'll discover that customer community users don't have access to the AccountContactRole Object. Add debug statements to you code and look at the debug logs to understand what's happening. I think you'll discover that your community user is indeed being created. You've not provided the rest of your code for me or others to anticipate other issues you might have with it.
    – crmprogdev
    Jun 15, 2015 at 13:04

You should have the IDs of the member and the community you want him to join. In Salesforce, with API 28 or higher, the community Sobject is called 'Network'.

In a similar way of making a user a subscriber of an object via Chatter (using the object EntitySubscription) you will need to create a NetworkMember object, that will receive those IDs I mentioned earlier. See the docs here.

  • can you provide small example on this..atleast the last part(user)
    – Pratap M
    Apr 1, 2017 at 17:26
  • @user533 sorry but my answer was incorrect. It is not possible to create a NetworkMember using Apex code. It is only possible to update, query and describe it. My assumption is that this table row is created for every active user in your organization when you have a "network" (like a community) set up on it. Apr 1, 2017 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .