+ Start a Discussion

Visualforce - get a field inside lookup

I am new in salesforce, so I hope i will explain well.

I have a list inside my class:

AttLst = new List<Attendance__c>([SELECT id,Name,Exam__c, FROM Attendance__c]);

The Exam__c is a lookup object, Inside the Exam Object i have Name and Datetime__c field.

I want to get in visualforce the Datetime field.

  <apex:pageBlockTable value="{!AttLst }" var="at">
          <apex:column value="{!at.Name}" />
           <apex:column value="{!at.Exam__c}" /> // here i get the name of the exam
            <apex:column value="{!at.Exam__c.Datetime__c}" /> // Here i get an error.

How can i do it? maybe changing the SOQL or maybe I didn't syntax error...

Please help me understand. Thank you.
Best Answer chosen by Asaf14
Naga  AlapatiNaga Alapati
Hi Nohi,

You need to query for those related object fields. Update the SOQL query like this
List<Attendance__c> AttLst = new List<Attendance__c>([SELECT id,Name,Exam__c, Exam__r.Name, Exam__r.Datetime__c FROM Attendance__c]);

In Visualforce page, you can get the datetime value as shown below:
<apex:pageBlockTable value="{!AttLst }" var="at">
          <apex:column value="{!at.Name}" />
           <apex:column value="{!at.Exam__r.Name}" /> 
            <apex:column value="{!at.Exam__r.Datetime__c}" />