function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ali MeyerAli Meyer 

Custom search page not working

My code is pretty much right off of Jeff Douglas's wonderful blog (http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/) and while it compiles just fine, the search doesn't actually work. I think there's something wrong with my SQL query because the dynamic search isn't altering it (as you can see in the debug log)--I think it's something related to the doSearch but I just can't figure it out.

Anyone want to give it a shot?:

Class:
public with sharing class RClassTest {

  // 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 RClassTest() {
    soql = 'select firstname, lastname, Id, account.name, CMI_Leadership__c, account.BillingStreet, account.BillingCity, account.BillingState 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 firstName = Apexpages.currentPage().getParameters().get('firstname');
    String lastName = Apexpages.currentPage().getParameters().get('lastname');
    String accountName = Apexpages.currentPage().getParameters().get('accountName');
    String CMILeadership = Apexpages.currentPage().getParameters().get('CMILeadership');
    String billingCity = Apexpages.currentPage().getParameters().get('billingCity');
    String billingState = Apexpages.currentPage().getParameters().get('billingState');

    soql = 'select firstname, lastname, account.name, account.BillingStreet, account.BillingState, account.BillingCity, CMI_Leadership__c from contact where account.name != null';
    if (!firstName.equals(''))
      soql += ' and firstname LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
    if (!lastName.equals(''))
      soql += ' and lastname LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';
    if (!accountName.equals(''))
      soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\''; 
    if (!CMILeadership.equals(''))
      soql += ' and CMI_Leadership__c includes (\''+CMILeadership+'\')';
    if (!billingCity.equals(''))
      soql += ' and account.BillingCity LIKE \''+String.escapeSingleQuotes(billingCity)+'%\'';
    if (!billingState.equals(''))
      soql += ' and account.BillingState LIKE \''+String.escapeSingleQuotes(billingState)+'%\'';

    // run the query again
    runQuery();

    return null;
  }

  // use apex describe to build the picklist values
  public List<String> CMILeadership {
    get {
      if (CMILeadership == null) {

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

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

      }
      return CMILeadership;         
    }
    set;
  }

}

Page:
<apex:page controller="RClassTest" sidebar="false">

  <apex:form >
  <apex:pageMessages id="errors" />

  <apex:pageBlock title="Search the Referral Database" 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("firstName").value,
          document.getElementById("lastName").value,
          document.getElementById("accountName").value,
          document.getElementbyId("billingCity").value,
          document.getElementbyId("billingState").value,
          document.getElementById("CMILeadership").options[document.getElementById("CMILeadership").selectedIndex].value
          );
      }
      </script>

      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="firstName" value="" />
          <apex:param name="lastName" value="" />
          <apex:param name="accountName" value="" />
          <apex:param name="billingCity" value="" />
          <apex:param name="billingState" value="" />
          <apex:param name="CMILeadership" value="" />
      </apex:actionFunction>

      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">First Name<br/>
        <input type="text" id="firstName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Last Name<br/>
        <input type="text" id="lastName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Account<br/>
        <input type="text" id="accountName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">City<br/>
        <input type="text" id="billingCity" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">State<br/>
        <input type="text" id="billingState" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">CMI Leadership<br/>
          <select id="CMILeadership" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!CMILeadership}" var="CMI">
              <option value="{!CMI}">{!CMI}</option>
            </apex:repeat>
          </select>
        </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="First Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="firstName" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputlink value="/{!contact.id}">
                <apex:outputField value="{!contact.firstName}"/>
                </apex:outputlink>
            </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:outputlink value="/{!contact.id}">
                <apex:outputField value="{!contact.lastName}"/>
                </apex:outputlink>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Account" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="account.name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputlink value="/{!contact.account.id}">
                <apex:outputField value="{!contact.account.name}"/>
                </apex:outputlink>
            </apex:column>
           
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Address" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="account.BillingStreet" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.account.BillingStreet}"/>
            </apex:column>
           
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="City" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="account.BillingCity" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.account.BillingCity}"/>
            </apex:column>
           
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="State" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="account.BillingState" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.account.BillingState}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="CMILeadership" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="CMI_Leadership__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.CMI_Leadership__c}"/>
            </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>

Thank you so much!!
Avidev9Avidev9
Did you tried to have a look @ debug log ? Like by printing the Dynamic Query ?
Ali MeyerAli Meyer
The debug log on the Visualforce page doesn't change when information is entered into the search fields--is that what you mean?
Ali MeyerAli Meyer
Just want to check in- can anyone help me out here?

Thank you!!