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
mtbdaysmtbdays 

Retrieving values through a field of type reference

Is it possible to retreive values from the referenced object of a field that is a lookup field on another object (ie. the datatype of the field is reference)?
 
My situation is:  I created a custom field on Opportunity called product__c that is a lookup field to Products.  In my Visualforce page, I can retrieve the Product name, but I want to retrieve the product ID too.  In the schema browser, I can see that in the Opportunity object, there is a custome field called product__c which has a reference to the Product2 object, and I see the Product2 fields under it, but I can not seem to retreive it in the Visualforce page.
 
I tried:  <apex:outputField value="{!Opportunity.Product__c.Product2.Id}"/>   but got an error.
 
Thanks 
jwetzlerjwetzler
{!Opportunity.Product__c} should be the id, it's just that since you're passing it to outputField it's rendering it the way lookup fields are rendered in their outputFormat, which is with a link and the name of the object.

if you use outputText (or no component at all, just the expression {!Opportunity.Product__c} ), it should give you what you need.  I think you can also refer to the relationship using __r, so something like {!Opportunity.Product__r.id}.