You need to sign in to do that
Don't have an account?
Dynamic Search in Visualforce Page
Hi All,
Can any one help me my search was not working properly
Controller:
Visualforce Page:
Thanks in Advance
Can any one help me my search was not working properly
Controller:
public with sharing class ContactSearchController { // the soql without the order and limit private String soql {get;set;} // the collection of contacts to display public List<Contact> contacts {get;set;} // the current sort direction. defaults to asc public String sortDir { get { if (sortDir == null) { sortDir = 'asc'; } return sortDir; } set; } // the current field to sort by. defaults to last name public String sortField { get { if (sortField == null) {sortField = 'lastName'; } return sortField; } set; } // format the soql for display on the visualforce page public String debugSoql { get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; } set; } // init the controller and display some sample data when the page loads public ContactSearchController() { soql = 'SELECT Name,Account.Name,Email,OtherCity,OtherCountry,OtherState,Phone FROM Contact where account.name != null'; runQuery(); } // toggles the sorting of query from asc<-->desc public void toggleSort() { // simply toggle the direction sortDir = sortDir.equals('asc') ? 'desc' : 'asc'; // run the query again runQuery(); } // runs the actual query public void runQuery() { try { contacts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'); } catch (Exception e) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!')); } } // runs the search with parameters passed via Javascript public PageReference runSearch() { String name = Apexpages.currentPage().getParameters().get('name'); String AccountName = Apexpages.currentPage().getParameters().get('AccountName'); String email = Apexpages.currentPage().getParameters().get('email'); String city = Apexpages.currentPage().getParameters().get('city'); String state = Apexpages.currentPage().getParameters().get('state'); String country = Apexpages.currentPage().getParameters().get('country'); String phone = Apexpages.currentPage().getParameters().get('phone'); soql = 'SELECT Name,Account.Name,Email,OtherCity,OtherCountry,OtherState,Phone FROM Contact where name != null'; if (!name.equals('')) soql += ' and name LIKE \''+String.escapeSingleQuotes(name)+'%\''; if (!AccountName.equals('')) soql += ' and Account.Name LIKE \''+String.escapeSingleQuotes(AccountName)+'%\''; if (!email.equals('')) soql += ' and Email LIKE \''+String.escapeSingleQuotes(email)+'%\''; if (!city.equals('')) soql += ' and OtherCity LIKE \''+String.escapeSingleQuotes(city)+'%\''; if (!state.equals('')) soql += ' and OtherState LIKE \''+String.escapeSingleQuotes(state)+'%\''; if (!country.equals('')) soql += ' and OtherCountry LIKE \''+String.escapeSingleQuotes(country)+'%\''; if (!phone.equals('')) soql += ' and Phone LIKE \''+String.escapeSingleQuotes(phone)+'%\''; // run the query again runQuery(); return null; } // runs the search with parameters passed via Javascript /* public PageReference runSearch() { String Name = Apexpages.currentPage().getParameters().get('Name'); String AccountName = Apexpages.currentPage().getParameters().get('AccountName'); String email = Apexpages.currentPage().getParameters().get('email'); String city = Apexpages.currentPage().getParameters().get('city'); String state = Apexpages.currentPage().getParameters().get('state'); String country = Apexpages.currentPage().getParameters().get('country'); String phone = Apexpages.currentPage().getParameters().get('phone'); soql = 'SELECT Name,Account.Name,Email,OtherCity,OtherCountry,OtherState,Phone FROM Contact where account.name != null'; if (!Name.equals('')) soql += ' and name LIKE ''+String.escapeSingleQuotes(Name)+'%''; if (!accountName.equals('')) soql += ' and account.name LIKE ''+String.escapeSingleQuotes(accountName)+'%''; if (!email.equals('')) soql += ' and email LIKE ''+String.escapeSingleQuotes(email)+'%''; if (!city.equals('')) soql += ' and city LIKE ''+String.escapeSingleQuotes(city)+'%''; if (!state.equals('')) soql += ' and state LIKE ''+String.escapeSingleQuotes(state)+'%''; if (!country.equals('')) soql += ' and country LIKE ''+String.escapeSingleQuotes(country)+'%''; if (!phone.equals('')) soql += ' and phone LIKE ''+String.escapeSingleQuotes(phone)+'%''; // run the query again runQuery(); return null; }*/ }
Visualforce Page:
<apex:page controller="ContactSearchController" sidebar="false"> <apex:form > <apex:pageMessages id="errors" /> <apex:pageBlock title="Contact Search" mode="edit"> <table width="100%" border="0"> <tr> <td width="200" valign="top"> <apex:pageBlock title="Parameters" mode="edit" id="criteria"> <script type="text/javascript"> function doSearch() { searchServer( document.getElementById("Name").value, document.getElementById("accountName").value, document.getElementById("email").value, document.getElementById("city").value, document.getElementById("State").value, document.getElementById("Country").value, document.getElementById("phone").value ); } </script> <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors"> <apex:param name="Name" value="" /> <apex:param name="accountName" value="" /> <apex:param name="email" value="" /> <apex:param name="city" value="" /> <apex:param name="state" value="" /> <apex:param name="country" value="" /> <apex:param name="phone" value="" /> </apex:actionFunction> <table cellpadding="2" cellspacing="2"> <tr> <td style="font-weight:bold;">Name<br/> <input type="text" id="firstName" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">Office Name<br/> <input type="text" id="accountName" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">Email<br/> <input type="text" id="email" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">City<br/> <input type="text" id="city" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">State<br/> <input type="text" id="state" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">Country<br/> <input type="text" id="country" onkeyup="doSearch();"/> </td> </tr> <tr> <td style="font-weight:bold;">Phone<br/> <input type="text" id="phone" onkeyup="doSearch();"/> </td> </tr> </table> </apex:pageBlock> </td> <td valign="top"> <apex:pageBlock mode="edit" id="results"> <apex:pageBlockTable value="{!contacts}" var="contact"> <apex:column > <apex:facet name="header"> <apex:commandLink value="Name" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="Name" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!contact.Name}"/> </apex:column> <!--<apex:column > <apex:facet name="header"> <apex:commandLink value="Last Name" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="lastName" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!contact.lastName}"/> </apex:column>--> <apex:column > <apex:facet name="header"> <apex:commandLink value="Office Name" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="account.name" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!contact.account.name}"/> </apex:column> <apex:column > <apex:facet name="header"> <apex:commandLink value="Email" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="Email" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!contact.Email}"/> </apex:column> <apex:column > <apex:facet name="header"> <apex:commandLink value="City" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="City" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!contact.OtherCity}"/> </apex:column> <apex:column > <apex:facet name="header"> <apex:commandLink value="State" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="State" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!contact.OtherState}"/> </apex:column> <apex:column > <apex:facet name="header"> <apex:commandLink value="Country" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="OtherCountry" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!contact.OtherCountry}"/> </apex:column> <apex:column > <apex:facet name="header"> <apex:commandLink value="Phone" action="{!toggleSort}" rerender="results,debug"> <apex:param name="sortField" value="Phone" assignTo="{!sortField}"/> </apex:commandLink> </apex:facet> <apex:outputField value="{!contact.Phone}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </td> </tr> </table> <apex:pageBlock title="Debug - SOQL" id="debug"> <apex:outputText value="{!debugSoql}" /> </apex:pageBlock> </apex:pageBlock> </apex:form> </apex:page>
Thanks in Advance
All Answers
http://abhithetechknight.blogspot.in/2013/07/dynamic-soql-brief-description.html
Let me know if you have further questions after implementation of DYNAMIC SOQL
You would be better just using public variables in your controller with getter and setter methods.
Add to your controller class:
public String name {get; set;}
Then in your runSearchMethod, get rid of all the parameter stuff and just use the variables from the class - they will get the values passed back by the page via any action.
Then in your visualforce page change your input fields like this:
<apex:inputField value="{!name}">
<apex:actionSupport event="onkeyup" action="{!runSearch}" rerender="results,debug,errors" />
</apex:inputField>
Then you can get rid of your javascript and actionfunction