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
magandrezmagandrez 

Nested SOQL question

Hi,

 

I need to retrieve some data using a nested SOQL, but I can't get the needed data. The SOQL is the following:

 

SELECT (SELECT Name,
               Unit_Price__c, 
               Quantity__c, 
               Time__c 
        FROM Valitut_Tuotteet__r)
FROM Service_Reservation__c 
WHERE Room_Reservation__c =: roomRes

 I need to extract the Name, Unit_Price__c, Quantity__c and Time__c and show them on a table (apex:repeat). Do you have any idea of how can I access that data?

 

Using <apex:repeat> and retrieving the fields doesn't work, I guess is because the data structure that the query creates is not a simple List<Service_Reservation__c>. 

 

Any idea on this?

 

Greetings,

 

MGA.

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The SOQL query will return a list of Service_Reservation__c objects, but each of those will contain a nested list of Valitut_Tuotteet__c objects.

 

Thus you'll need to iterate the nested list.  Something like the following:

 

<apex:repeat value="{!myList}" var="roomres">
  <apex:repeat value="{!roomres.Valitut_Tuotteets__r}" var="vt">
     <apex:outputField value="{!vt.Name}"/>
  </apex:repeat>
</apex:repeat>

 

All Answers

bob_buzzardbob_buzzard

The SOQL query will return a list of Service_Reservation__c objects, but each of those will contain a nested list of Valitut_Tuotteet__c objects.

 

Thus you'll need to iterate the nested list.  Something like the following:

 

<apex:repeat value="{!myList}" var="roomres">
  <apex:repeat value="{!roomres.Valitut_Tuotteets__r}" var="vt">
     <apex:outputField value="{!vt.Name}"/>
  </apex:repeat>
</apex:repeat>

 

This was selected as the best answer
magandrezmagandrez

Hi bob,

 

As usual, you are right :)

 

Many thanks, 

 

MGA.

KoellerDKoellerD

Thank you very much for this!  Enjoy the Kudos.

 

I was trying to figure out how to access this data for much too long!

 

 

Nathan HincheyNathan Hinchey
How can you access this information in Apex? It seems like it should be obvious, but I can't figure it out.