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
liwuliwu 

Display lookup relationship in apex:pageTable

I have an custom object which has a lookup relationship to User (I call this field UserReference). I have a visualforce page that displays several fields for each object in an apex:pageTable.

 

How do I display the lookup relationship in this table? I don't want to display the user ID, I want to display the name.

 

I would like to do something like:

 

<apex:column >
    <apex:facet name="header">User namet</apex:facet>
    {!myObject.UserReference__c.Name}
</apex:column>

 

but that clearly doesn't work.

 

I hope I don't have to loop through the list of myObjects in the controller and for each object perform a lookup into the user table to retrieve the name and then somehow make that available to the pageTable.

 

Anyone help me out?

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi,

 

you have to use UserReference__r.Name instead of UserReference__c.Name see below code.

 

<apex:column >
    <apex:facet name="header">User namet</apex:facet>
    {!myObject.UserReference__r.Name}
</apex:column>

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator

All Answers

hitesh90hitesh90

Hi,

 

you have to use UserReference__r.Name instead of UserReference__c.Name see below code.

 

<apex:column >
    <apex:facet name="header">User namet</apex:facet>
    {!myObject.UserReference__r.Name}
</apex:column>

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator

This was selected as the best answer
liwuliwu

But that gives me an error:

 

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

 

I do include UserReference__c in my SELECT statement (in the controller) but I can't change this UserReference__r because that won't compile saying: 

 

Compile Error: No such column 'UserReference__r' on entity 

 

 

EDIT: I finally found a useful doc .. I added it as UserReference__r.Name in my SELECT and this works

 

hitesh90hitesh90

Hi,

 

you have to add UserReference__r.Name in your SOQL query.

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator