You need to sign in to do that
Don't have an account?
rubel 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:
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>
You should not set the countries, because it's blank. so you need use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Can you please tell me, how can I get this selected value ( Items ) in controller ?
I need to use the selected value in this query
But, not working.
Thanks a lot,
Rubel Rana
Please find the working code below
visualforce page
Controller
I hope this will help.
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.
Thanks
It worked in my org with the code I provided. Please make sure you copy the entire code as is.
What's the update?