You need to sign in to do that
Don't have an account?
Jayakrishnan Salim
Testing user login in communities
Hi,
I would like to create a test class where I need to test the details of logged in user in SF community. What I am doing is, I take the userId of the logged in user and then fetch the contactId from user object. And with that contact Id, I fetch the account id associated with it. But when I try to assign contact id to the user object in test class, I get the below exception.
Here is my test class code.
Thank you in advance.
I would like to create a test class where I need to test the details of logged in user in SF community. What I am doing is, I take the userId of the logged in user and then fetch the contactId from user object. And with that contact Id, I fetch the account id associated with it. But when I try to assign contact id to the user object in test class, I get the below exception.
System.DmlException: Update failed. First exception on row 0 with id 0050W0000070R3kQAE; first error: UNKNOWN_EXCEPTION, You can't create a contact for this user because the org doesn't have the necessary permissions. Contact Salesforce Customer Support for help.: []
Here is my test class code.
@isTest public class CommunityHomeControllerTest { @testSetup static void recordCreation() { UserRole r = new UserRole(name = 'TEST ROLE'); insert r; User user = new User(); user.ProfileID = [Select Id From Profile Where Name='System Administrator'].id; user.EmailEncodingKey = 'ISO-8859-1'; user.LanguageLocaleKey = 'en_US'; user.TimeZoneSidKey = 'America/New_York'; user.LocaleSidKey = 'en_US'; user.FirstName = 'first'; user.LastName = 'last'; user.Username = 'test@appirio1.com'; user.CommunityNickname = 'testUser123'; user.Alias = 't1'; user.Email = 'no@email.com'; user.IsActive = true; user.PortalRole = 'Manager'; insert user; } static testMethod void getTotalReports(){ Test.StartTest(); User user = [Select id, contactId from user where Username = 'test@appirio1.com']; System.RunAs(user) { Account a = new Account(Name='Test Account Name'); insert a; Contact c = new Contact(LastName = 'Contact Last Name'); c.AccountId = a.Id; insert c; user.ContactId = c.id; update user; PageReference pageRef = Page.CommunityHome; Test.setCurrentPage(pageRef); CommunityHomeController controller = new CommunityHomeController(); controller.getParentAccounts(); Test.stopTest(); } } }
Thank you in advance.
All Answers
Can you change the profile to Community profile and try to associate the contact with the user.
--
Thanks,
Prashant