function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jayakrishnan SalimJayakrishnan 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.
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. 
Best Answer chosen by Jayakrishnan Salim
Jayakrishnan SalimJayakrishnan Salim
I could resolve the issue myself. Here is the code. 
 
@isTest
public class CommunityHomeControllerTest {
    static testMethod void getTotalReports(){         
        Account a = new Account(Name='Test Account Name');
        insert a;           
        Contact c = new Contact(LastName = 'Contact Last Name');
        c.AccountId = a.Id;
        insert c;  
        System.runAs ( new User(Id = UserInfo.getUserId()) ) {
            UserRole r = [select id from UserRole where name = 'CEO'];
            UserLicense licence = [SELECT Id FROM UserLicense where name ='Partner Community'];
            
            Profile p = [SELECT Id FROM Profile WHERE UserLicenseId = : licence.Id Limit 1]; 
            User comUser = new User(alias = 'test123', email='test123@noemail.com',
                                    emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                                    localesidkey='en_US', profileid = p.Id, country='United States',IsActive =true,
                                    contactId=c.Id,
                                    timezonesidkey='America/Los_Angeles', username='newone@noemail.com');
            insert comuser;
        }
        User cuser = [select id from user where username ='newone@noemail.com' limit 1];
        System.RunAs(cuser) {
            PageReference pageRef = Page.CommunityHome;
            Test.setCurrentPage(pageRef);
            CommunityHomeController controller = new CommunityHomeController();
            controller.getParentAccounts();
        }         
    }
}

 

All Answers

Prashant Pandey07Prashant Pandey07
Hi Jaya,

Can you change the profile to Community profile and try to associate the contact with the user.

--
Thanks,
Prashant
Jayakrishnan SalimJayakrishnan Salim
I could resolve the issue myself. Here is the code. 
 
@isTest
public class CommunityHomeControllerTest {
    static testMethod void getTotalReports(){         
        Account a = new Account(Name='Test Account Name');
        insert a;           
        Contact c = new Contact(LastName = 'Contact Last Name');
        c.AccountId = a.Id;
        insert c;  
        System.runAs ( new User(Id = UserInfo.getUserId()) ) {
            UserRole r = [select id from UserRole where name = 'CEO'];
            UserLicense licence = [SELECT Id FROM UserLicense where name ='Partner Community'];
            
            Profile p = [SELECT Id FROM Profile WHERE UserLicenseId = : licence.Id Limit 1]; 
            User comUser = new User(alias = 'test123', email='test123@noemail.com',
                                    emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
                                    localesidkey='en_US', profileid = p.Id, country='United States',IsActive =true,
                                    contactId=c.Id,
                                    timezonesidkey='America/Los_Angeles', username='newone@noemail.com');
            insert comuser;
        }
        User cuser = [select id from user where username ='newone@noemail.com' limit 1];
        System.RunAs(cuser) {
            PageReference pageRef = Page.CommunityHome;
            Test.setCurrentPage(pageRef);
            CommunityHomeController controller = new CommunityHomeController();
            controller.getParentAccounts();
        }         
    }
}

 
This was selected as the best answer