I have a custom object called "Availability" that has a couple of field that I'd like to display and I want to be able to filter them by a date range selected by the user through the VisualForce page and then be grouped by a field called "Day_Date__c". Basically, for each day there are 3 Availability records, so I'd like them to be grouped by the Day/Date so that they're easier to view.

So far, I've figured out how to filter a list of Availability records by Next 7 Days or Next 5 Days using a radio selection as well as by the region, which is great. I've also figured out separately how to group Availability records by the Day/Date field, but I cannot figure out how to do both!

How can I apply my date range and region filter but also display the output of records grouped by a field? Code for my visualforce page and controller for the filtering portion is below - any help is greatly appreciated!


    public with sharing class AvailabilityController {
    public Availability__c availability{get;set;}
    public list<Availability__c> listOfAvailabilities{get;set;}
    public Date startdate{get;set;}
    public Date enddate{get;set;}
    public String region;
    public AvailabilityController() {

        availability = new Availability__c();

    public Integer dateoption{get;set;}
    public List<SelectOption> getItems() {
       List<SelectOption> dateoption = new List<SelectOption>(); 
       dateoption.add(new SelectOption('7','Next 7 Days')); 
       dateoption.add(new SelectOption('5','Next 5 Days')); 
       return dateoption; 

    public PageReference displayingTable(){
        region = availability.Region__c;
        listOfAvailabilities = Database.query('SELECT id, Name, Availabilities__c, Time_Slot__c FROM Availability__c WHERE Region__c =: region AND Date__c = Next_N_Days:' + dateoption);
        return null;

VisualForce Page:

<apex:page id="myPage" controller="AvailabilityController" extensions="AvailabilitiesbyDateExtension">
    <apex:form id="myForm">
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                        <apex:selectRadio title="Select Date Range" value="{!dateoption}">
                        <apex:selectOptions value="{!items}" />
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Region: "/>
                    <apex:inputField value="{!availability.Region__c}"/>
                <apex:commandButton value="Display" action="{!displayingTable}" reRender="tableId"/>
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!listOfAvailabilities}" var="record" id="tableId">
                    <apex:column value="{!record.Name}" headerValue="Availability Name"/>
                    <apex:column value="{!record.Availabilities__c}" headerValue="Availabilities"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.