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
Laytro80Laytro80 

Custom List view with a dynamic search

Hi,

 

Struggling on this, I would like a custom list view with a dynamic date field search.  Below is a visualforce page which is all set-up just how I want it. I am using the action save clearly this needs to be a search method.

 

 

<apex:page standardController="Reservation__c"  extensions="Resmanager">
    <apex:form >
        <apex:sectionHeader title="Availability Search"/>            
            <apex:pageBlock title="Search">
                <apex:pageBlockButtons >
                    <apex:commandButton action="{save}" value="Search"/>
                </apex:pageBlockButtons>
                <apex:inputField value="{!Reservation__c.Start_Date__c}"/>
            </apex:pageBlock>  
            <apex:pageBlock title="Reservations">
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!Reservations}" var="res">
                        <apex:column value="{!res.Start_Date__c}"  />
                        <apex:column value="{!res.End_Date__c}"  />
                        <apex:column value="{!res.Nights__c}"  />
                    </apex:pageBlockTable>
                </apex:pageBlockSection>   
            </apex:pageBlock> 
    </apex:form>
</apex:page>

 

I am not sure how to write the search method, my attempt on the controller below.  I am hardcoding yesterday to get the controller to work.

 

 

public class Resmanager { 

    public Resmanager(ApexPages.StandardController controller) {

    }
        
    public List<Reservation__c> getReservations(){
        List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where start_date__c = yesterday];
        return listBd;   
    }
   
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

Change line

reservation=controller.getRecord();

 

to

 

reservation=(Reservation__c)controller.getRecord();

All Answers

Bhawani SharmaBhawani Sharma

Create a getter and setter for  Reservation object and initialize it with controller.

Now instead of Yesterday, you can use :

 

reservation.Start_Date__c.

 

On click of Save , you ony need to refresh the result section. This will call the reservation getter and  will will display the refreshed result.

 

 

laytro1978laytro1978

Thanks for the post.

 

I very new to APEX and have only had to use it couple of times.

 

I tried without any success to apply get and set to the code but I keep getting a ton of errors.

 

Are you able to provide some sample code?

 

No problem if not thanks for your help.

 

R

Bhawani SharmaBhawani Sharma

Use this :

 

 

public class Resmanager {
public Reservation__c resaervation(get;set;)
 public Resmanager(ApexPages.StandardController controller) { reservation=controller.getRecord();
} public List<Reservation__c> getReservations(){ List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where start_date__c = reservation.Start_Date__c]; return listBd; }

public void save()
{
getReservation();
 }

 

 

 

Laytro80Laytro80

Thanks for this, have tried still getting problems.

 


Error: Resmanager Compile Error: Illegal assignment from SObject to SOBJECT:Reservation__c at line 4 column 12

 

 

 

Apex Controller

Made some changes to your code highlighted in red.  

 

public class Resmanager {
    public Reservation__c reservation {get;set;}
    public Resmanager(ApexPages.StandardController controller) {
           reservation=controller.getRecord();
             }
            
    public List<Reservation__c> getReservations(){
            List<Reservation__c> listBd=[select r.id, r.Start_Date__c, R.End_Date__c, nights__c from Reservation__c r where start_date__c = :reservation.Start_Date__c];
            return listBd;   
        }
    public void save()
    {
    getReservation();
     }
}

 

Visualforce 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:inputField value="{!Reservation__c.Start_Date__c}"/>
            </apex:pageBlock>  
            <apex:pageBlock title="Reservations">
                <apex:pageBlockSection >
                    <apex:pageBlockTable value="{!Reservations}" var="res">
                        <apex:column value="{!res.Start_Date__c}"  />
                        <apex:column value="{!res.End_Date__c}"  />
                        <apex:column value="{!res.Nights__c}"  />
                    </apex:pageBlockTable>
                </apex:pageBlockSection>   
            </apex:pageBlock> 
    </apex:form>
</apex:page>

 

 

 


 

 

Bhawani SharmaBhawani Sharma

Change line

reservation=controller.getRecord();

 

to

 

reservation=(Reservation__c)controller.getRecord();
This was selected as the best answer
laytro1978laytro1978

Thanks for all your help a pleasure working with you.  Got there in the end and thanks to your help I now understand how to create these types of APEX solutions.