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
MrBungleMrBungle 

SObject row was retrieved via SOQL without querying the requested field: User.AccountId

Can someone help me figure out why this test class is failing?

 

The class was needed for a visualforce page that Customer Portal users will be hit with when they initiate a Live Agent Chat. The page is called a Pre-Chat page. I couldn't get access to the Customer Portal User's account name using the fields from the User standard controller.

 

Here's the Class:

public with sharing class NICP_Functions 
{

    public String getAccountName(){return strAccountName;}
    private User u;
    string strAccountName = '';
    
    public NICP_Functions(ApexPages.StandardController controller) 
    {
        this.u = (User)controller.getRecord(); 
        strAccountName = u.AccountId;
    }

}

 Here's the test class:

@isTest
private class TestNICP_Functions
{
    static testMethod void testPreChat()   
    {
        // PAGE REFERENCE
        Profile p = [SELECT Id FROM Profile WHERE Name =: 'Customer Portal Manager Standard'];
        User u = [SELECT id FROM User WHERE ProfileId =: p.Id AND IsActive = true AND AccountId != null LIMIT 1];
        PageReference ref = Page.NICP_PreChat;
        Test.setCurrentPage(ref);         
        NICP_Functions controller = new NICP_Functions(new ApexPages.StandardController(u));
        string an = controller.getAccountName();
    }
}

The error I get when I test the execution:

Class TestNICP_Functions 
Method Name testPreChat 
Pass/Fail Fail 
Error Message System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.AccountId 
Stack Trace Class.NICP_Functions.<init>: line 22, column 1
Class.TestNICP_Functions.testPreChat: line 22, column 1 

 

 

Thank you very much!

Starz26Starz26

In the VF page add:

 

<apex:outputField value="{!User.AccountID}" rendered="false"/>

 

Since you are using standard controller you have to actually use the field on the page to have it available in the getRecord method

MrBungleMrBungle

Thank you very much for your quick reponse!

 

I tried this and I still got the same error.

Sunbeam11Sunbeam11
User u = [SELECT id, Account Id FROM User WHERE ProfileId =: p.Id AND IsActive = true AND AccountId != null LIMIT 1];

Make sure to include the accoutId in your SOQL and you should be good.