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
hoomelhoomel 

Accessing Fields from Subselect

Hi,

 

we are currently a bit stuck at referencing the correct data that fit our needs.

 

Currently we are rendering a new line in calendar for any Rental Period in the database. The Rental Period contains a relationship to another custom object, which holds the product the customer has rented.

 

Right now, a new line is rendered for every Rental Period, regardless of periods that have the same product.

 

We would like to be able to do a subselect query to get every period start date for every product.

The subselect currently looks like this:

 

Select (Select Start__c, End__c From Rental_System__r) From Rental_Systems__c

 

From this subselect, we need to access specific fields.

I tried accessing Start__c on the VIsualforce page and in the Apex controller as you would do it for a 'normal' select query but that was not very successfull ;)

Is it in any way possible to directly access the values in the query?

 

Regards,

hoomel

 

Best Answer chosen by Admin (Salesforce Developers) 
hoomelhoomel

I figured out how to access the fields i needed:


The page:

 

<apex:repeat value="{!verleih}" var="t">
     <a href="/{!t.Id}" title="System Details">{!t.System_Name__c}</a>
          <apex:repeat value="{!t.Rental_System__r}" var="rs">
             <a href="/{!rs.Id}"" title="{!rs.Rental_System__r.System_Name__c}">
          </apex:repeat>
</apex:repeat>

 The class:

 

rent_all = [Select Id, Name, System_Name__c,(Select Id,Account__r.Name,Rental_System__r.System_Name__c,Start__c, End__c, Type__c From Rental_System__r) From Rental_Systems__c order by Id];

public List<Rental_Systems__c> getVerleih(){
    return rent_all;
}

 

 

As you can see, I accessed the fields of the subselect with nested <apex:repeat>-tags.

Maybe someone can use this.


Regards,

hoomel

All Answers

cloudgofercloudgofer

you need to do something like below to get child object rows in a list.

 

 

public with sharing class SubQuerySample {

    public void exeA() {
        List<Account> accounts = [SELECT Account.Name, (SELECT Contact.FirstName, Contact.LastName FROM Account.Contacts) FROM Account];
        for (Account ac : accounts) {
            if (ac.contacts != null || ac.contacts.size() > 0 ) {
                List<Contact> contacts = ac.contacts ;	
                System.debug(LoggingLevel.INFO, contacts);
            }
        }
    }
}

 



 

 

hope this helps !!

hoomelhoomel

Is it correct, that i only get one record into the 'contacts' list ?

 

When i try to add every contact to a list in the for loop, i get an error 'List has no rows for assignment to SObject' .

 

Just to be sure that this is what i need:

I would like to have a list of Rental Periods. 

The only limitation is, that i need when i retrieve the list, i want to have only one entry for every Rental System, which contains all the Rental Periods with all their start and end dates.

 

Can you achieve this somehow?

hoomelhoomel

I figured out how to access the fields i needed:


The page:

 

<apex:repeat value="{!verleih}" var="t">
     <a href="/{!t.Id}" title="System Details">{!t.System_Name__c}</a>
          <apex:repeat value="{!t.Rental_System__r}" var="rs">
             <a href="/{!rs.Id}"" title="{!rs.Rental_System__r.System_Name__c}">
          </apex:repeat>
</apex:repeat>

 The class:

 

rent_all = [Select Id, Name, System_Name__c,(Select Id,Account__r.Name,Rental_System__r.System_Name__c,Start__c, End__c, Type__c From Rental_System__r) From Rental_Systems__c order by Id];

public List<Rental_Systems__c> getVerleih(){
    return rent_all;
}

 

 

As you can see, I accessed the fields of the subselect with nested <apex:repeat>-tags.

Maybe someone can use this.


Regards,

hoomel

This was selected as the best answer