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
guppyguppy 

Basic question on accessing fields in "related" (lookup) objects

I'm almost embarassed to ask this but I've searched, read the docs, etc. and have what seems like a very simple question.

 

In our setup, we have a case which can be linked to an Opportunity (via a lookup field).  really though, this could be any object.  What I want to do is pull some info from the linked opp to display on the case page layout.  Having done some VF work before on the Solutions controller, seems like a logical choice..however, I cannot find for the life of me how to get the related fields.  Here's my "code":

 

<apex:page standardController="Case" > Case number: {!case.CaseNumber} <br/> Opp Name: {!case.Opportunity__c} </apex:page>

 

The problem of course is that the "Opp name" gives the salesforce ID.  What I'd like to say is:

 

Opp Name: {!case.Opportunity__c.Name}

 

 

But of course that fails.  So the question is...how do I get access to the related object data fields from within my current VF page when using the "case" controller?

 

Thanks!

 

D

Best Answer chosen by Admin (Salesforce Developers) 
guppyguppy

Found the answer but it took some digging and wasn't obvious.  Basically, it seems for each lookup object there is the lookup key/id (which is accessed using the API field name) but then there's another field you can access which takes you right to the linked object (which has an __r suffix).  This code got what I needed (well, I can now build on this):

 

<apex:page standardController="Case" > Case number: {!case.CaseNumber} <br/> Opp Name: {!case.Opportunity__r.Name} </apex:page>

 

The tool that helped me find this was "Apex Explorer" which shows the "real" fields and the "super-secret" fields.

 

Posting here in the hope this will help someone else.