I created a visualforce page for Communities users to update their username and email. In real life the page works fine, logging in as a Community user I can update my username and email. However, I can't get the test to pass, because it is throwing the following error:

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Here is the test method:

@isTest static void testChangeUserName() {
        User portalUser = getPortalUser();

        System.runAs(portalUser) {    
            mportal_PortalAccountController controller = new mportal_PortalAccountController();
            controller.userAccount.Username = 'TEST_NAME1231241@TEST.COM';

    portalUser = [SELECT Id, Username FROM User WHERE ID = :portalUser.Id];
    System.assertEquals('TEST_NAME1231241@TEST.COM', portalUser.UserName.toUpperCase()); //username not changed

Test user setup:

private static User getPortalUser() {
    Contact c = new Contact(LastName = 'Test');
    Insert c;

    Profile p           = [SELECT Id FROM Profile  WHERE Name       = 'CRCPortal' LIMIT 1];

    User u = new User(Alias             = 'standt', 
                      Email             = 'standardudsfaser@testorg.com', 
                      EmailEncodingKey  = 'UTF-8', 
                      LastName          = 'Testing', 
                      LanguageLocaleKey = 'en_US', 
                      LocaleSidKey      = 'en_US', 
                      ProfileId         =  p.Id, 
                      TimeZoneSidKey    = 'America/Los_Angeles', 
                      UserName          = 'standarduser1234@testorg.com', 
                      ContactId         =  c.id
    insert u;
    return u;

Controller save method:

public void saveEmailUserName() {
            try {
                update userAccount;
                showChangeEmail = false;
            } catch (Exception e) {
                System.debug(LoggingLevel.ERROR, e.getMessage());

Giving the test user a System Administrator profile lets the test pass, but obviously I want to test as a communities user.


  • I'm having the same issue. The fun part is: my test passes if I compile the class in API 33.0, but fails when using API 34.0. Have you been able to solve this? – Pablo Venturino Aug 28 '15 at 0:38
  • Any update on this?I am having the same issue in API 35.0. – Thys Michels Feb 1 '16 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.