+ Start a Discussion
Jay KanchanaJay Kanchana 

Retrieving Parent Field in SOQL and Displaying on Visual Force page

Hello!

Currently we display a pageblocktable that  lists child records on a visual force page. We would like to display the parent name as well for each record.

for example we have 
Parent__c
Child__c
Lookup__c


Current Query is something like
[SELECT id,  Deliverable__c,
                Description__c, Parent__c
                FROM Child__c 
                WHERE Lookup__c=:Lookup_Id
               ];  

We want to add the Parent__c.Name to the SOQL.

FollowingSOQL works, butthe visual force page displays the error that the data is being displayed without retrieving. Visualforce page code is something like -   {!c.Parent__r.Name} .
[SELECT id,  Deliverable__c,
                Description__c, Parent__r.Name
                FROM Child__c 
                WHERE Lookup__c=:Lookup_Id
               ];  


This gives a soql error that the relationship does not exist.
[SELECT id,  Deliverable__c,
                Description__c, (select Name from Parent__r)
                FROM Child__c 
                WHERE Lookup__c=:Lookup_Id
               ];  

Wither my SOQL is wrong or may be there is a different way to display the parent name in the visual force page?

Thank you!
Jay
sharathchandra thukkanisharathchandra thukkani
query 'SELECT id,  Deliverable__c,
                Description__c, Parent__r.Name
                FROM Child__c 
                WHERE Lookup__c=:Lookup_Id'  should work. other query is wrong..you cant get the parent values in the sub query..
YuchenYuchen
Using "Parent__r.Name" in the query looks good. Could you print some debug logs for the output of that query and see what is printing? Also, you can try to save your Apex Class to the server again because sometimes there may be problem with saving.