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
Michael Wood 24Michael Wood 24 

Displaying Lookup Data as a name in visualforce

Hi Everyone, 

I'm working on a pdf export for Opportunities using a Visualforce page. I am able to grab all the data I need but I can't figure out how to display my lookup data fields properly in the export. 

Here is the instance of the lookup field being used:
User-added image

It is pulling data from Contacts:
User-added image

and in the export it displays as the ID and not the name:
User-added image
I have tried using ".name" in my code: <apex:outputText value="{!Opportunity.Designer_Name__c.Name}"/>

but I get this error: Error: Unknown property 'String.Name'

Any help would be greatly appreciated, thank you!
Best Answer chosen by Michael Wood 24
Dushyant SonwarDushyant Sonwar
Why are you not using outputField tag?
<apex:outputField value="{!Opportunity.Designer_Name__c}"/>

Is it not working?

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Michael,

By default the lookup field gives an id you need to fetch and then show it in the visualforce page.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Michael Wood 24Michael Wood 24
Thanks for the answer Anutej, is there a way I can display the .name string and not just the ID of the field? I'm new to Visualforce and Apex and have been searching for any sort of relevant posts or documentation but I'm struggling to figure this out. Any help or relevant posts/articles would be incredibly useful. 
ANUTEJANUTEJ (Salesforce Developers) 
>> https://stackoverflow.com/questions/35177469/salesforce-unknown-property-string-name

As used in the example in the above link can you try fetching them once in the controller and then using them in the visualforce page.

Similar error: https://developer.salesforce.com/forums/?id=906F000000098V1IAI
Dushyant SonwarDushyant Sonwar
Why are you not using outputField tag?
<apex:outputField value="{!Opportunity.Designer_Name__c}"/>

Is it not working?
This was selected as the best answer
Michael Wood 24Michael Wood 24
Thank you Dushyant Sonwar, this is exactly what I needed. This fixed everything.