You need to sign in to do that
Don't have an account?
MrBungle
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!
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
Thank you very much for your quick reponse!
I tried this and I still got the same error.
Here is the fix
http://www.oyecode.com/2013/04/apex-standardcontrollergetrecord.html