+ Start a Discussion
Brad BothBrad Both 

Accessing fields from a lookup relationship between a child and another object

Let me do my best to explain this properly. I'm in the middle of creating a controller class, using that controller I will be creating a visualforce page that will be creating PDFs using this controller.

User-added image 
Above is the way my current set up looks. I have my Parent object called Programs__c, within Programs__c is multiple fields, a lookup relationship and 3 children (Program_Toggle__c, Program_IO__c, Program_Details__c). For the sake of my document creation I will need to access all of the fields within Program__c as well as it's children. So far so easy. 

Now looking into the three children, they each have their own fields and one (or two) look up relationship to either Program_Toggle_Option__c, Program_IO_Function__c, or Program_Details_Variable__c (Which matches to which should be obvious). There are 3 fields in each of these objects and I need to display all of them. Every piece of literature I find gives me ways to access grandchildren of parents, but when I try and follow these instructions I run into the issues where the relationship between the Child and Lookup Relationship cannot be found. 

This was the primary resource I used, to help create my controller. Which in all honesty at this point is basically a copy paste with all variables changed to properly reflect my objects to see if it will work. Replacing Member with Program, Interest with Program_Toggle and Equipment with Program_Toggle_Option In my controller I'm only trying to access the fields from the Program_Toggle__c lookup relationship with Program_Toggle_Option__c and I'm receiving this error. 

Error: Compile Error:
Didn't understand relationship
 'Program_Toggle_Options__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

If any other information is required let me know. 

Anupama SamantroyAnupama Samantroy
Hi Brad,

If you want to access the child object details from the parent then you use the child relationship name.
i.e Select id, name, (select id from Toggles__r) from Program_Toggle_Option__c because in your schema Program _Toggle_Option__c is the parent and the Progran_Toggle__c is it's child.
To find the exact name of the child relationship please see the lookup field on the Program_Toggle__c object.