+ Start a Discussion
rubel rana 6rubel rana 6 

Run a query from value of dropdown list

Hello everyone !!

I can run a query , which gets value from date picker, and returns me values in date range. It works fine. But, now I want to get the value of SObject name from dropdown list and run the same query. But, my select value of drowpdown is not coming in controller. Can you please help me? Stuck in many days.
Please run the code, you can understand. 

Controller:
public class ContactsByDateController {
public List<Account> cts {get;set;}
public Date fromDate {get;set;}
public Date toDate {get;set;}
String[] countries = new String[]{};    
string c {get;set;}
string objName {get;set;}

String query;
public ContactsByDateController() {
    
}
    /* dropdown list start 
     * */
       public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Account','Account'));
        options.add(new SelectOption('Contacts','Contacts'));
        options.add(new SelectOption('Jobs__c','JObs'));
        options.add(new SelectOption('My_leads__c','My Leads'));
        options.add(new SelectOption('My_Customers__c','My Customers'));
        
        return options;
    }
    
     public String[] getCountries() {
        return countries;
    }

    
    
    public void setCountries(String[] countries) {
        this.countries = countries;
    }
    
    /* dropdown list ends 
     * */

    /* variable assigning for selected list from dropdown*/
    
public void datepick1() {
Datetime fromDT = Datetime.newInstance(fromDate.year(), fromDate.month(), fromDate.day());
Datetime toDT = Datetime.newInstance(toDate.year(), toDate.month(), toDate.day());
objName = c;
String formattedFromDate = fromDT.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');
String formattedToDate = toDT.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');

if ( objName  == null ) {
 query = 'SELECT Name  FROM Account';
}
else {
 query = 'SELECT Name   FROM ' +objName;
}

query += ' WHERE CreatedDate >= ' + formattedFromDate;
query += ' AND CreatedDate <= ' + formattedToDate;
System.debug(query);
cts = Database.query(query);
}
public void clearResults() {
cts.clear();
}
}

Vsf page :

<apex:page docType="html-5.0" controller="ContactsByDateController" >
<apex:form >
<apex:input value="{! fromDate }" type="date" />
<apex:input value="{! toDate }" type="date" />
<apex:commandButton value="Search Contacts" action="{!datepick1}"/>
<apex:commandButton value="Clear Contacts" action="{!clearResults}"/>
<apex:selectList value="{!countries}" multiselect="false" size="3">
            <apex:selectOptions value="{!items}"/>
</apex:selectList><p/>
<apex:pageBlock title="Results in Range">
<apex:pageBlockTable value="{!cts}" var="ct">
<apex:column value="{!ct.Name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
mukesh guptamukesh gupta
Hi Rubel,

You should not set the countries, because it's blank. so you need use below code:-
 
<apex:page docType="html-5.0" controller="ContactsByDateController" >
<apex:form >
<apex:input value="{! fromDate }" type="date" />
<apex:input value="{! toDate }" type="date" />
<apex:commandButton value="Search Contacts" action="{!datepick1}"/>
<apex:commandButton value="Clear Contacts" action="{!clearResults}"/>
<apex:selectList size="3">
            <apex:selectOptions value="{!Items}"/>
</apex:selectList><p/>
<apex:pageBlock title="Results in Range">
<apex:pageBlockTable value="{!cts}" var="ct">
<apex:column value="{!ct.Name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
rubel rana 6rubel rana 6
Thanks lot @mukesh gupta,
Can you please tell me, how can I get this selected value ( Items ) in controller ?
<apex:selectList size="3"> <apex:selectOptions value="{!Items}"/> </apex:selectList><p/>

I need to use the selected value in this query
 
query = 'SELECT Name   FROM ' +Items;

But, not working. 
Thanks a lot,
Rubel Rana
rubel rana 6rubel rana 6
I have tried like this way.
public void setItems(String Items)
    {
        this.Items = Items;
    }

 
Abdul KhatriAbdul Khatri
Hi rubel,

Please find the working code below

visualforce page
 
<apex:page docType="html-5.0" controller="ContactsByDateController" >
    <apex:form >
        <apex:input value="{!fromDate }" type="date" />
        <apex:input value="{!toDate }" type="date" />

        <apex:commandButton value="Search Contacts" action="{!datepick1}"/>
        <apex:commandButton value="Clear Contacts" action="{!clearResults}"/>

        <apex:selectList value="{!ObjName}" multiselect="false" size="3">
            <apex:selectOptions value="{!items}"/>
        </apex:selectList><p/>
    
        <apex:pageBlock title="Results in Range">
            <apex:pageBlockTable value="{!cts}" var="ct">
                <apex:column value="{!ct.Name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller
public class ContactsByDateController 
{
    public List<SObject> cts {get;set;}
    public Date fromDate {get;set;}
    public Date toDate {get;set;}
    public string ObjName {get;set;}
    String query;

    public ContactsByDateController() {
    
    }

    /* dropdown list start 
     * */
       public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Account','Account'));
        options.add(new SelectOption('Contacts','Contacts'));
        options.add(new SelectOption('Jobs__c','Jobs'));
        options.add(new SelectOption('My_leads__c','My Leads'));
        options.add(new SelectOption('My_Customers__c','My Customers'));
        
        return options;
    }
    
    public void setObjName(String selectedItem) {
        this.objName = selectedItem;
    }
    
    /* dropdown list ends 
     * */

    /* variable assigning for selected list from dropdown*/
    
    public void datepick1() {
        Datetime fromDT = Datetime.newInstance(fromDate.year(), fromDate.month(), fromDate.day());
        Datetime toDT = Datetime.newInstance(toDate.year(), toDate.month(), toDate.day());
        String formattedFromDate = fromDT.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');
        String formattedToDate = toDT.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');

        if ( objName  == null ) {
             query = 'SELECT Name  FROM Account';
        }
        else 
        {
             query = 'SELECT Name FROM ' + objName;
        }

        query += ' WHERE CreatedDate >= ' + formattedFromDate;
        query += ' AND CreatedDate <= ' + formattedToDate;
        System.debug(query);
        cts = Database.query(query);
    }
    
    public void clearResults() {
        cts.clear();
    }
}

I hope this will help.
rubel rana 6rubel rana 6

Thanks @Abdul Khatri,
It worked when I use  object name like Account here, but it doesn't work if I use Sobject here. It shows an error Error: Unknown property 'SObject.Name' .  
Can you help me to this error? I also know, SObject should work for all object, but I am not expert, why its not wokring here.

public List<SObject> cts {get;set;}

Thanks
Abdul KhatriAbdul Khatri
Hi rubel,

It worked in my org with the code I provided. Please make sure you copy the entire code as is.
Abdul KhatriAbdul Khatri
Hi Rubel,

What's the update?