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
ikamsfikamsf 

Displaying nested custom object fields in visual page

I have a custom object House__c with a lookup field which is another custom object and it's called House_Address__c and it is from custom object of Address__c.

I am using standard controller for House__c and recordsetvar houses in my visualforce page, but this is not working to display the Zipcode__c from the Address__c object.

Do I need to extend the controller and add code to do this? If yes, please share some instructions/tutorials for the solution. Thanks!
 
<apex:pageBlockTable value="{!houses}" var="item">
            <apex:column value="{!item.House_Name__c}"/>
            <apex:column value="{!item.House_Address__c}"/>
            <apex:column value="{!item.House_Address__c.Zip__c}"/>
</apex:pageBlockTable>



 
Best Answer chosen by ikamsf
ikamsfikamsf
Actually, there is no need to the extension cotroller!

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi ikamsf,

You need add to your soql query House_Address__c.Zip__c field.
 
house = [SELECT Id, House_Name__c, House_Address__c, House_Address__c.Zip__c FROM House__c  WHERE...];

Thanks,
Alex
ikamsfikamsf
I am an asp.net developer with my second day in SF. Could you please let me know where this needs to be added to?
Alexander TsitsuraAlexander Tsitsura
Can you share you controller? 
ikamsfikamsf
I don't have a controller, it's the standard controller whcih I don't know any way to see the file. I have only created two custom objects and one visualforce page. the code for the page is below. Do I need to write an extension controller for House__c standard controller?
 
<apex:page standardController="House__c" recordSetvar="houses" sidebar="false" >
  <h1>Houses with Address</h1>
    <apex:pageBlock title="Houses">
        <apex:pageBlockTable value="{!houses}" var="item">
            <apex:column value="{!item.House_Name__c}"/>
            <apex:column value="{!item.House_Address__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

 
Alexander TsitsuraAlexander Tsitsura
You need create custom extension and add it to apex:page tag
 
<apex:page standardController="House__c" extensions="HouseExtension"
and create extension
public with sharing class HouseExtension {

    private List<House__c> houses {get; set;}

    public HouseExtension(ApexPages.StandardSetController controller){
        //Use the standard controller's get records method
        houses = (List<House__c>)controller.getRecords();
        
        houses = [SELECT ID, House_Name__c, House_Address__c,       
                         House_Address__c.Zip__c
                    FROM House__c
                   WHERE ID IN: houses];
    }
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
 
ikamsfikamsf
Now I am getting this error message:

Didn't understand relationship 'House_Address__c' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name.
Alexander TsitsuraAlexander Tsitsura
Oh sorry, please use code below
 
public with sharing class HouseExtension {

    private List<House__c> houses {get; set;}

    public HouseExtension(ApexPages.StandardSetController controller){
        //Use the standard controller's get records method
        houses = (List<House__c>)controller.getRecords();
        
        houses = [SELECT ID, House_Name__c, House_Address__c,       
                         House_Address__r.Zip__c
                    FROM House__c
                   WHERE ID IN: houses];
    }
}

thanks,
alex
ikamsfikamsf
I had to do a little change to the code above:

 
public with sharing class HouseExtension {

    private List<House__c> houses {get; set;}

    public HouseExtension(ApexPages.StandardSetController controller){
        //Use the standard controller's get records method
        houses = (List<House__c>)controller.getRecords();
        
        houses = [SELECT ID, House_Name__c, House_Address__c, House_Address__r.Zip__c
                    FROM House__c];
    }
}
and in visualforce page:
 
<apex:column value="{!item.House_Address__r.Zip__c}"/>


 
ikamsfikamsf
Actually, there is no need to the extension cotroller!
This was selected as the best answer