+ Start a Discussion
Vigneshwaran LoganathanVigneshwaran Loganathan 

Problem with Dynamic Search

I have created a page for dynamic search using Using jeffdouglas blog wit hsome modifications. Its not working form me. Am i wrong Anywhere?

CLASS:

public with sharing class vki2{

 private String soql {get;set;}
 
  public List<Contact> contacts {get;set;}

 
  public String sortDir {
    get  { if (sortDir == null) {  sortDir = 'desc'; } return sortDir;  }
    set;
  }

 
  public String sortField {
    get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
    set;
  }

 
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir; }
    set;
  }

 
  public vki2() {
    soql = 'select Name, Leadsource from contact';
    runQuery();
  }

 
  public void toggleSort() {
    
    sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
   
    runQuery();
  }

 
  public void runQuery() {

    try {
      contacts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir);
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Oops!'));
    }

  }

    public PageReference runSearch() {

    String Name= Apexpages.currentPage().getParameters().get('Name');
     String Leadsource= Apexpages.currentPage().getParameters().get('Leadsource');
    
    soql = 'select Name,Leadsource from contact';
    
    if (!Name.equals(''))
      soql +=  ' and Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
       if (!Leadsource.equals(''))
      soql += ' and Leadsource LIKE \''+String.escapeSingleQuotes(Leadsource)+'%\'';
  
    runQuery();

    return null;
  }

 
  public List<String> Leadsource{
    get {
      if (Leadsource== null) {

        Leadsource= new List<String>();
        Schema.DescribeFieldResult field = Contact.Leadsource.getDescribe();

        for (Schema.PicklistEntry f : field.getPicklistValues())
          Leadsource.add(f.getLabel());

      }
      return Leadsource;          
    }
    set;
  }

}


VF:

<apex:page controller="vki2" sidebar="false">
  <apex:form >
  <apex:pageMessages id="errors" />
  <apex:pageBlock title="Find!" 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("Leadsource").options[document.getElementById("Leadsource").selectedIndex].value
          );
      }
      </script>
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="Name" value="" />
          <apex:param name="Leadsource" value="" />
      </apex:actionFunction>
      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">Name<br/>
        <input type="text" id="Name" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Leadsource<br/>
          <select id="leadsource" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!Leadsource}" var="category">
              <option value="{!category}">{!category}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>
      </apex:pageBlock>
    </td>
    <td valign="top">
    <apex:pageBlock mode="edit" id="results">
        <apex:pageBlockTable value="{!contacts}" var="o">
            <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="{!o.Name}"/>
            </apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Leadsource" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Leadsource" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!o.Leadsource}"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
    </td>
  </tr>
  </table>
  </apex:pageBlock>
  </apex:form>
</apex:page>

Thank U
 
Best Answer chosen by Vigneshwaran Loganathan
nickwick76nickwick76
Hi Vigneshwaran, 
I found two problems with your code.

First , in the Javascript 'doSearch' method in your Visualforce page you are referring to an id that doesn't exist since it doesn't match. The select id is 'Leadsource' and you are looking for 'leadsource'. I.e. case-sensitive.

Should be 
searchServer(
          document.getElementById("Name").value,
         
          document.getElementById("leadsource").options[document.getElementById("leadsource").selectedIndex].value
          );
The second issue is in the controller in the 'runSearch' method. The SOQL-query gets a wrong syntax. The SOQL-query lacks a 'where' condition and starts with an 'and'. Should probably be:
soql = 'select Name,Leadsource from contact';
    
    if (!Name.equals(''))
      soql +=  ' where Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
       if (!Leadsource.equals(''))
      soql += ' and Leadsource LIKE \''+String.escapeSingleQuotes(Leadsource)+'%\'';

Your code works for me when I have made these changes. 

// Niklas
 

All Answers

PratikPratik (Salesforce Developers) 
Hi Vigneshwaran,

If you have some modification, then please check once for the modification. The blog code is working fine.

Thanks,
Pratik
nickwick76nickwick76
Hi Vigneshwaran, 
I found two problems with your code.

First , in the Javascript 'doSearch' method in your Visualforce page you are referring to an id that doesn't exist since it doesn't match. The select id is 'Leadsource' and you are looking for 'leadsource'. I.e. case-sensitive.

Should be 
searchServer(
          document.getElementById("Name").value,
         
          document.getElementById("leadsource").options[document.getElementById("leadsource").selectedIndex].value
          );
The second issue is in the controller in the 'runSearch' method. The SOQL-query gets a wrong syntax. The SOQL-query lacks a 'where' condition and starts with an 'and'. Should probably be:
soql = 'select Name,Leadsource from contact';
    
    if (!Name.equals(''))
      soql +=  ' where Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
       if (!Leadsource.equals(''))
      soql += ' and Leadsource LIKE \''+String.escapeSingleQuotes(Leadsource)+'%\'';

Your code works for me when I have made these changes. 

// Niklas
 
This was selected as the best answer
Vigneshwaran LoganathanVigneshwaran Loganathan
Thanks for u all :)