+ Start a Discussion
Tate PriceTate Price 

Querying parent records in a controller

Hi, 

I am attempting to query parent records from a custom VF page within a child record. I have Desks as a parent record of Reservations with the following values:

Desks
Office - Picklist
Desks - Dependent Picklist
Status - Picklist

Reservations
Desk - parent look-up
Start Date
End Date

My VF page contains a search area for querying reservations that match my input criteria. However, I cannot seem to pull in the picklist options from my Desks object as part of my search. 

Here is the code from my VF page:
 
<apex:page standardController="Reservation__c"  extensions="Resmanager">
    <apex:form >
        <apex:sectionHeader title="Availability Search"/>            
            <apex:pageBlock title="Search">
                <apex:pageBlockButtons >
                    <apex:commandButton action="{!reservation}" value="Search"/>
                </apex:pageBlockButtons>
                <apex:pageBlockSection columns="2">
                    <apex:inputField value="{!Reservation__c.Start_Date__c}"/>
                    <apex:inputField value="{!Reservation__c.End_Date__c}"/> 
                    
                </apex:pageBlockSection>
            </apex:pageBlock>  
            <apex:pageBlock title="Reservations">
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!Reservations}" var="res">
                        <apex:column value="{!res.Office_Desk__c}"  />
                        <apex:column value="{!res.Start_Date__c}"  />
                        <apex:column value="{!res.End_Date__c}"  />
                        
                        
                    </apex:pageBlockTable>
                </apex:pageBlockSection>   
            </apex:pageBlock> 
    </apex:form>
</apex:page>

Here is my controller:
 
Public class Resmanager {

    public PageReference reservation() {
        return null;
    }

    public Reservation__c reservation {get;set;}
    public Resmanager(ApexPages.StandardController controller) {
           reservation=(Reservation__c)controller.getRecord();
             }
            
    public List<Reservation__c> getReservations(){
            List<Reservation__c> listBd=[select Id, Name, Start_Date__c, End_Date__c, Office_Desk__c from Reservation__c r 
                where  (Start_Date__c >= :reservation.Start_Date__c 
                    and 
                    Start_Date__c <= :reservation.End_Date__c
                    )
                    or
                    (End_Date__c >= :reservation.Start_Date__c
                    and
                    End_Date__c <= :reservation.End_Date__c)];  
            return listBd;   
        }
For the SOQL loop, I only want to return the desks that 
a) are reserved
b) reserved within the specified date ranges
c) match the office the User is going to search

How do I structure the query within the VF page (for searching) and how do I construct a SOQL query to extract the matching records? 
This is my second day inside of Apex and my knowledge of SOQL is limited. 

Any assistance would be greatly appreciated as I have been searching without any luck. 
hitesh90hitesh90
Hello,

Please find below syntax for using SOQL with date filter.

Apex Class:
Public class Resmanager {  
    public PageReference reservation() {
        return null;
    }    
    public Reservation__c reservation {get;set;}
    public Resmanager(ApexPages.StandardController controller) {
        reservation=(Reservation__c)controller.getRecord();
    }    
    public List<Reservation__c> getReservations(){
        List<Reservation__c> listBd=[select Id, Name, Start_Date__c, End_Date__c, Office_Desk__c from Reservation__c r 
        where  (Start_Date__c >= :reservation.Start_Date__c and End_Date__c <= :reservation.End_Date__c)];  
        return listBd;   
    }
}

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
Tate PriceTate Price
My date filter was actually working as expected. 

My primary question was about the SOQL search on the parent object (e.g. querying records from the Desk object through the Reservation object). 

By making use of the Status field inside of the Desk object, I want to allow the User to search the available desks for the date range they select. (There is also an Office picklist field that I want to use but I figure once I get the syntax for the first, the second should be simpler.)

Does that make sense? Is this even possible?