public with sharing class WebService{

    public Account acc;

    public WebService(ApexPages.StandardController stdController){
        this.acc = (Account)stdController.getRecord();
        System.debug('account name' + this.acc.name);


    public PageReference sendStructure{
        return null;

When I try to debug and see what is inside this.acc , I see Id, RecordTypeId, IsPersonAccount but when I try to get the value of this.acc.name. I get the error as stated above. Can you help me understand why I am getting this error and what should I do inorder to get the value of the name or any other feilds for that matter ? Thanks in advance

Here is my visualforce page

<apex:page standardController="Account" extensions="Webservice" action="{!sendStructure}" LightningStylesheets="true">
    <apex:pageMessages escape="false"></apex:pageMessages>
  • is it person account? can you try this.acc.FirstName ? Jun 10, 2019 at 9:27
  • Yes, it is a person account and I still get the error. I am also trying to some other custom fields in account and I always get this error. Jun 10, 2019 at 9:29
  • can you remove this bit action="{!sendStructure} and try? Jun 10, 2019 at 9:35

1 Answer 1


Standard controller instance only has values of those fields (except few fields) which are displayed in visualforce page. As you are not displaying any fields on visualforce page, that field value wont be available for you to use from standard controller's getRecord method.

You can use either of the below two options to get the value of the required field in controller without having to do a separate query.

By adding field to visualforce page but not displaying it in the page, you can use any of the visualforce components which can bind the standard controller's record's instance. Eg : <apex:outputText>, <apex:outputField>, <apex:inputField>, apex:inputText>, <apex:inputHidden>, <apex:variable> are few to mention.

<apex:outputText rendered="false" value="{!Account.Any_Account_Field__c}" />

Or by using standard controller's add fields method, but using this method will require you to skip the unit test wherever you will be using that field.

if (!Test.isRunningTest()) stdController.addFields(new List<String>{'Any_Account_Field__c'}); //can add other fields that are needed
this.acc = (Account)stdController.getRecord();

You can replace Any_Account_Field__c with the field name which you want to access in controller.

One other way of handling this is to perform a SOQL query on the object and get all the required fields.

this.acc = [SELECT Id, Any_Account_Field__c, .. From Account where Id =:stdController.getId()];
System.debug('account name' + this.acc.name);
  • Just commenting to draw extra attention to the fact that addFields() is very problematic for tests. It is an option, but the issues it has in unit testing makes it practically useless (you could add a field, but couldn't use it anywhere in your controller/extension). Grabbing the recordId and querying the record (and pulling your target fields) is a better option.
    – Derek F
    Jun 10, 2019 at 17:44
  • <apex:variable .../> also works as does apex:inputHidden; I tend to use the former
    – cropredy
    Jun 10, 2019 at 17:44
  • Hey @DerekF Thanks for the pointers, added a note mentioning about issues with addFields method during unit testing. I earlier included only two approaches, as those two didn't need any additional SOQL's. Anyhow, updated the answer with the 3rd approach. Jun 11, 2019 at 6:05
  • Hey @cropredy, Thanks for the comment, actually I just posted an example, anyhow, now I updated the answer with the components that can be used for this purpose. Jun 11, 2019 at 6:08
  • Thanks for the update guys. Jun 11, 2019 at 6:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .