+ Start a Discussion
Sharad Jain 9Sharad Jain 9 

Display records based on picklist selection and between dates

Hello Friends,
I need to display records on pageblock table based on below conditions on show button:
1. select object from Picklist like Account or Lead
2. Todate (calendar Control)
3. Fromdate (Calendar Control)
When I select account frompicklist and select date range between todate and fromdate and click on show button then all the account records created between selected date should be displayed in pageblock table.
Can anyone help me accomplishing this task?

Thanks
Sharad Jain 9Sharad Jain 9
Thanks Vonid for your reply but I am new to this so Can anyone gie me the apex code?
Maharajan CMaharajan C
Hi Sharad,

You can use the below code reference for your scenario.

Am not done any UI validation while click the button so you have to choose all the value in the UI.

===============
Visualforce Page:
===============

<apex:page controller="DynamicObjectController"  docType="html-5.0" sidebar="false">
    
    <apex:form id="pageForm">
        
        <apex:pageMessages />
        
        <apex:pageBlock Title="Filter">
            
            <apex:pageBlockSection >
                
                <apex:pageBlockSectionItem >
                    
                    
                    
                    <apex:selectList label="Select Object Name" title="Select Object Name" multiselect="false" value="{!selectedSObject}" size="1">
                        
                        <apex:selectOptions value="{!SObjectNames}"/> 
                        
                    </apex:selectList>
                    
                </apex:pageBlockSectionItem>
                
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <apex:outputLabel value="From Date: " for="Fromdate"/>
                
                <!-- From Date: <apex:input type="date" value="{!dat}"/> -->
                
                <apex:input value="{!startdate}"  html-placeholder="Start Date" type="date" /> 
                
                <!-- <apex:inputText value="{!startdate}" size="10" id="demo" onfocus="DatePicker.pickDate(false, this , false);" /> -->
                
                <apex:outputLabel value="To Date: " for="todate"/> 
                
                <apex:input value="{!enddate}"  html-placeholder="End Date" type="date" />
                
                <!-- <apex:inputText value="{!enddate}" size="10" id="dem" onfocus="DatePicker.pickDate(false, this , false);" />  -->
                
                <!--  To Date: <apex:input type="date" value="{!todat}"/>  -->
                
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                
            </apex:pageBlockSection> 
            
                <apex:pageBlockButtons location="bottom">
                    <apex:commandButton value="Get Records" action="{!fetchRecords}" reRender="PB1" />
                </apex:pageBlockButtons>
                
        </apex:pageBlock>
        
        <apex:pageblock id="PB1">
            <apex:pageBlockTable value="{!lstObjs}" var="rec" Title="Records">
                <apex:repeat value="{!lstFlds}" var="fld">
                    <apex:column value="{!rec[fld]}"/>
                </apex:repeat>
            </apex:pageBlockTable>        
        </apex:pageblock>
        
    </apex:form>
    
</apex:page>


===============
Apex Class :
===============


public class DynamicObjectController {
    
    public List<SelectOption> getSObjectNames() {
        
        List<SelectOption> sObjectNames = new List<SelectOption>();
        
        sObjectNames.add(new SelectOption('Select sObject', 'Select sObject'));
        
        for ( Schema.SObjectType type : Schema.getGlobalDescribe().values() ) {
            
            sObjectNames.add(new SelectOption(type.getDescribe().getName(), String.valueOf(type)));//Fetch object name and add in selectoption list
            
        }
        
        return sObjectNames;
        
    }
    
    public pagereference fetchRecords()
    {
        
        
        Datetime stdate = datetime.newInstance(startdate.year(), startdate.month(), startdate.day());
        Datetime eDate = datetime.newInstance(enddate.year(), enddate.month(), enddate.day());
        
        String startDateTime = stdate.format( 'yyyy-MM-dd' ) + 'T00:00:00.000Z';
        String endDateTime = eDate.format( 'yyyy-MM-dd' ) + 'T00:00:00.000Z';
        
        system.debug(startDateTime);
        system.debug(endDateTime);
        
        string query='SELECT Id,Name,Createddate FROM ' + selectedSObject ;
        system.debug('@@@ query '+ query);
        
        if(startdate !=null)
            query+=' where CreatedDate > '+startDateTime ;
        if(enddate!=null  && startdate!=null)
            query+=' AND CreatedDate < '+endDateTime ;
        system.debug('@@@ query '+ query);
        
        lstObjs = Database.query(query);
        lstFlds = new List<String>();
        Set<string> setFlds = new Set<String>();
        for(integer i=0;i<lstObjs.size();i++){
            setFlds.addAll(lstObjs[i].getPopulatedFieldsAsMap().keySet());
        }
        
        lstFlds.addAll(setFlds);
        system.debug(lstFlds);                 
        
        return null;
    }
    
    
    
    public String selectedSObject { get; set; }
    
    public Date dat {get;set;}
    
    public Date todat {get;set;}
    
    public date startdate {get;set;}
    
    public date enddate {get;set;}
    
    public List<sObject> lstObjs{get;set;}
    
    public List<string> lstFlds {get;private set;}
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C