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
Shawn ReichnerShawn Reichner 

Using VF Page and Class to Show Custom Object records on Account Page Layouts

Team,

I have created the following Apex Class Extension and VF Page which when placed on our Account Page Layouts gives me the following error...

Content cannot be displayed: SObject row was retrieved via SOQL without querying the requested field: Account.Account_Name__c

I am a little confused as I am a beginner with coding, but I am thinking I am requesting the correct field.  

Please help me by looking over my code as follows... Thank you for your time, and I thank you in advance to whoever can save the day! 

Shawn


Apex Class

/*
Class Name:
Description: This Class is used to display Bill To Address details in Account page as in-line visual force page.
Date Created: 12-11-2014.
*/

public class PopulatebilltoInfoHandler
{
    Account ac= new Account();
    List<Bill_to_Address__c> roomLoclist = new List<Bill_to_Address__c>();
    
    
    private Integer counter=0;
    private Integer list_size=2000;
    public Id accId;
    private Integer total_size;
    
    public PopulatebilltoInfoHandler(ApexPages.StandardController controller)
    {
        ac = (Account)controller.getRecord();
        accId = ac.Account_Name__c;
        //populateBilltoInfo();
        total_size = [Select count() From Bill_to_Address__c Where (Bill_to_Address__c.Account_Name__c = :ac.Account_Name__c)];
    }
    
    public List<Bill_to_Address__c> getroomList()
    {
        try
        {
            roomLoclist = [Select Id,Name,Customer_Number__c,City__c,State__c,Zip__c
                                     From Bill_to_Address__c]; 
                                    
        }
        catch(Exception e){system.debug('Query Exception'+e.getMessage());}
        if(roomLoclist != null && roomLoclist.size()>0)
            return roomLoclist;
        else
            return roomLoclist;
       
    }
    
     public PageReference Previous()
    {
        counter-=list_size;
        return null;
    }
    
    public PageReference Next()
    {
        counter+=list_size;
        return null;
    }
    
    public Boolean getDisablePrevious()
    {
        if(counter >0)
            return false;
        else
            return true;
    }
    
    public Boolean getDisableNext()
    {
        if(counter+list_size < total_size)
            return false;
        else return true;
    }


}


VF Page Code

<apex:page standardcontroller="Account" extensions="PopulatebilltoInfoHandler" id="lolinePage">
<apex:stylesheet value="{!$Resource.pdfresources}"/>



<apex:pageBlock title="Bill-To Details">

    <apex:pageBlockTable value="{!roomList}" var="loline" id="lolinedetails">
        <apex:column >
            <apex:facet name="header">Bill To Address</apex:facet> 
                 <a href="/{!loline.Name}" target="_parent">{!loline.Name}</a>
        </apex:column>
        
        <apex:column >
                <apex:facet name="header">City</apex:facet>
                <apex:outputText value="{!loline.City__c}"/>
        </apex:column>
        
        <apex:column > 
                <apex:facet name="header">State</apex:facet>
                <apex:outputText value="{!loline.State__c}"/>
        </apex:column>
        
        <apex:column > 
                <apex:facet name="header">Zip</apex:facet>
                <apex:outputField value="{!loline.Zip__c}"/>
        </apex:column>
        
        
   

    </apex:pageBlockTable>

    
   
        

    
</apex:pageBlock>

  <apex:outputPanel id="myButtons"  layout="block">
    <apex:form >
        <apex:panelGrid cellpadding="7" columns="4">
        
            <apex:commandButton value="<Previous" action="{!Previous}" disabled="{!DisablePrevious}" reRender="lolinedetails,myButtons"/>
            <apex:commandButton value="Next>" action="{!Next}" disabled="{!DisableNext}" reRender="lolinedetails,myButtons"/>
        
        </apex:panelGrid>
    </apex:form>
    </apex:outputPanel>
</apex:page>
Fabien TaillonFabien Taillon
Hi Shawn,
When using getRecord() method, only the fields that are on your Visualforce page are queried. As on your page Account_Name__c is never referenced, value is not set by getRecord().
You can either query yourself the field, or add an hidden field so that the getRecord() method will get it for you:
<apex:inputHidden value="{!Account.Account_Name__c}"/>

https://www.salesforce.com/us/developer/docs/pages/Content/apex_ApexPages_StandardController_getRecord.htm (https://www.salesforce.com/us/developer/docs/pages/Content/apex_ApexPages_StandardController_getRecord.htm" target="_blank)
Balaji BondarBalaji Bondar
Hi Shawn,
You have retrieve the data like below.getRecord() will return you only Id and Name:
ac = (Account)controller.getRecord();
ac = [select Id, Name, Account_Name__c from Account where Id=:ac.Id LIMIT 1];
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you
SarfarajSarfaraj
Hi Shawn

Modify this line,
ac = (Account)controller.getRecord();
to this,
List<String> fields = new List<String>();
fields.add('Account_Name__c');
controller.addFields(fields);
ac = (Account)controller.getRecord();
--Akram
Shawn ReichnerShawn Reichner
Akram,

After inputting your suggestion, I am now getting a different error messgae...

Content cannot be displayed: Invalid id:

The first two options provided the same error as before, so this time it is different.  

I am reposting my code for your review.....Thank you so much everyone for your help, i think we are getting closer...

Apex Code

/*
Class Name:
Description: This Class is used to display Bill To Address details in Account page as in-line visual force page.
Date Created: 12-11-2014.
*/

public class PopulatebilltoInfoHandler
{
    Account ac= new Account();
    List<Bill_to_Address__c> roomLoclist = new List<Bill_to_Address__c>();
    
    
    private Integer counter=0;
    private Integer list_size=2000;
    public Id accId;
    private Integer total_size;
    
    public PopulatebilltoInfoHandler(ApexPages.StandardController controller)
    {

     List<String> fields = new List<String>();
          fields.add('Account_Name__c');
          controller.addFields(fields);
          ac = (Account)controller.getRecord();


        accId = ac.Account_Name__c;
        //populateBilltoInfo();
        total_size = [Select count() From Bill_to_Address__c Where (Bill_to_Address__c.Account_Name__c = :accId)];
    }
    
    public List<Bill_to_Address__c> getroomList()
    {
        try
        {
            roomLoclist = [Select Id,Name,Customer_Number__c,City__c,State__c,Zip__c
                                     From Bill_to_Address__c]; 
                                    
        }
        catch(Exception e){system.debug('Query Exception'+e.getMessage());}
        if(roomLoclist != null && roomLoclist.size()>0)
            return roomLoclist;
        else
            return roomLoclist;
       
    }
    
     public PageReference Previous()
    {
        counter-=list_size;
        return null;
    }
    
    public PageReference Next()
    {
        counter+=list_size;
        return null;
    }
    
    public Boolean getDisablePrevious()
    {
        if(counter >0)
            return false;
        else
            return true;
    }
    
    public Boolean getDisableNext()
    {
        if(counter+list_size < total_size)
            return false;
        else return true;
    }


}



VF Page Code

<apex:page standardcontroller="Account" extensions="PopulatebilltoInfoHandler" id="lolinePage">
<apex:stylesheet value="{!$Resource.pdfresources}"/>



<apex:pageBlock title="Bill-To Details">

    <apex:pageBlockTable value="{!roomList}" var="loline" id="lolinedetails">
        <apex:column >
            <apex:facet name="header">Bill To Address</apex:facet> 
                 <a href="/{!loline.Account_Name__c}" target="_parent">{!loline.Account_Name__c}</a>
        </apex:column>
        
        <apex:column >
                <apex:facet name="header">City</apex:facet>
                <apex:outputText value="{!loline.City__c}"/>
        </apex:column>
        
        <apex:column > 
                <apex:facet name="header">State</apex:facet>
                <apex:outputText value="{!loline.State__c}"/>
        </apex:column>
        
        <apex:column > 
                <apex:facet name="header">Zip</apex:facet>
                <apex:outputField value="{!loline.Zip__c}"/>
        </apex:column>
        
        
   

    </apex:pageBlockTable>

    
   
        

    
</apex:pageBlock>

  <apex:outputPanel id="myButtons"  layout="block">
    <apex:form >
        <apex:panelGrid cellpadding="7" columns="4">
        
            <apex:commandButton value="<Previous" action="{!Previous}" disabled="{!DisablePrevious}" reRender="lolinedetails,myButtons"/>
            <apex:commandButton value="Next>" action="{!Next}" disabled="{!DisableNext}" reRender="lolinedetails,myButtons"/>
        
        </apex:panelGrid>
    </apex:form>
    </apex:outputPanel>
</apex:page>
Fabien TaillonFabien Taillon
What is the type of your Account_Name__c field, is it a lookup ? If it's just a string (don't know, as the field label is "Name") you should change the type of your accId variable.
Shawn ReichnerShawn Reichner
Thank you for your reply!!! It is a text field What do I need to do to change the value??? Thank you again and I am sorry, again I am a newbie and this one is stumping me. I appreciate all of your help! Shawn
Fabien TaillonFabien Taillon
At least change "public Id accId;" to "public String accId;" Changing the name of the variable would be a good idea too as it's not an Id anymore.
But with only the name and not the Id, this won't work:
<a href="/{!loline.Account_Name__c}" target="_parent">{!loline.Account_Name__c}</a>
Maybe you should create a new lookup field and use it instead.