3

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;
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
user3197916
  • 379
  • 1
  • 7
  • 17

2 Answers2

1

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.

Renato Oliveira
  • 13,257
  • 10
  • 60
  • 135
  • can you provide small example on this..atleast the last part(user) – Pratap M Apr 01 '17 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. – Renato Oliveira Apr 01 '17 at 18:53
1

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.

crmprogdev
  • 40,955
  • 9
  • 58
  • 115
  • 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 – user3197916 Jun 15 '15 at 06: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 '15 at 13:04