I'm trying to build a visualforce page that will contain information from the Account object and a Custom Object. There is a Lookup Relationship from Account to the custom object. Retriving the fields from the Account object is not a problem. The problem is that when trying to get field values from the custom object using this:

<apex:pageBlockSection title="Custom Object Information" columns="2">
    <apex:pageBlockSectionItem >
        <apex:outputLabel value="Custom Object" for="custom_object"/>
        <apex:outputField id="account" value="{!Account.Custom_Object__r.name}"/>

I get nothing. I'm using the standardController Account.
<apex:page standardController="Account"> like I said, I have no problems getting fields values from the account to my visualforce page, but I can't get the data from the custom object.. :( .. Anyone knows why?

Thanks in advanced!!

  • Looks ok to me. Are you accessing the page with a valid id? e.g. /apex/myPage?id=a000006284. If not then the standardController won't be loading any data. Apr 8 '14 at 13:15
  • Are you getting any error messages? Apr 8 '14 at 13:16
  • Can you see fields from the related custom object? I would start there first and see if that will come through. Also, verify the records that you are testing with have the fields you are trying to retrieve. If you are not getting a compile error, I would guess the syntax is correct, but the data might not exist.
    – JimRae
    Apr 8 '14 at 14:01
  • First thing I would do... is ensure that Account.Custom_Object__c does indeed have a value. If it does not.... that means it won't be able to pull fields from an empty reference.
    – dphil
    Apr 8 '14 at 14:07
  • Hi JimRae and dphil. Yes, I can see all fields and the fields are there. :( The custom object do have values, so I think that this can't be he problem. The lookup relationship is from Account to the custom, that is ok. Can't understand what is the problem. Apr 8 '14 at 14:41

If the standardcontroller doesn't pick/query the referenced fields automatically you should be able to tell the standardcontroller to include them in an extensioncontroller with the addFields(List<String>) method of the standardcontroller.

The strings in fieldNames can either be the API name of a field, such as AccountId, or they can be explicit relationships to fields, such as foo_r.myField_c.

See the documentation here.

  • 2
    The standard controller does NOT pick up relationship fields. The farthest it goes down relationships is the __c of the relationship, or the Id of the lookup / md field. For any other relationship fields, you'll either need to requery the account in an extension controller, or you can try using addFields like Samuel mentioned. Apr 8 '14 at 13:19
  • Richard Durrant, yes, I'm using a valid id for the page. Alex Tennant, no, I get no erros. All is saved and when I run the page, I can only see the Account fields. Samuel De Rycke and James Loghry, are you guys saying that I need to build a extension just to pull data from those related fields? So, is this wrong information: wiki.developerforce.com/page/Visualforce_Extended_Print_Preview Apr 8 '14 at 13:27

Strange enough is that I can't access these fields from the BlockSection, but I can if I add a pageBlockTable inside the pageBlockSection. So what I did is this:

<apex:pageBlock > <apex:pageBlockSection title="CustomObj"> <apex:pageBlockTable value="{!Account.CustomObj__r}" var="itemObje"> <apex:column value="{!itemObje.Name}"/> <apex:column value="{!itemObje.CustomFiel1__c}"/> <apex:column value="{!itemObje.CustomFiel2__c}"/> <apex:column value="{!itemObje.CustomFiel3__c}"/> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock>

For me is working, hope is legal :) Thank you all for your help.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.