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
Kris HankinsKris Hankins 

Javascript search isn't functioning correctly

I have a VF page that I am embedding into a tab on another VF page to provide search functionality on a custom object that I created. The page renders correctly, there are no VF errors or runtime errors for the controller. What I am having difficulty finding is why the search function(s) are failing to initiate. The controller was modified from a controller I found online. It seems to work well initially, pulling in the rental rates as it should. The problem comes when you enter criteria into the additional search fields. Nothing happens.


Custom Controller Code
 
public with sharing class RateSearchController {

  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of Rates to display
  public List<Rental_Rate__c> rates {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 Name
  public String sortField {
    get  { if (sortField == null) {sortField = 'Name'; } return sortField;  }
    set;
  }

  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' ' + 'limit 200' ; }
    set;
  }

  // init the controller and display some sample data when the page loads
   public RateSearchController() {
    soql = 'select Name, Account__c, Active__c, Rental_Group__c, Discount_Level__c, Daily_Rate__c, Weekly_Rate__c, Monthly_Rate__c, Expiration_Date__c  from Rental_Rate__C ';
    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 {
      Rates = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' ' + 'limit 250');
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops! Something went wrong!'));
    }

  }

  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {

    String Name = Apexpages.currentPage().getParameters().get('Name');
    String Account = Apexpages.currentPage().getParameters().get('Account__c');
    String Active = Apexpages.currentPage().getParameters().get('Active__c');
    String Rental_Group = Apexpages.currentPage().getParameters().get('Rental_Group__c');
    String Daily_Rate = Apexpages.currentPage().getParameters().get('Daily_Rate__c');    
    String Weekly_Rate = Apexpages.currentPage().getParameters().get('Weekly_Rate__c');
    String Monthly_Rate = Apexpages.currentPage().getParameters().get('Montyly_Rate__c');
    String Expiration_Date = Apexpages.currentPage().getParameters().get('Expiration__Date');
    String Discounts = Apexpages.currentPage().getParameters().get('Discount_Level__c');
    

    soql = 'select Name, Account__c, Active__c, Discount_Level__c, Rental_Group__c, Daily_Rate__c, Weekly_Rate__c, Monthly_Rate__c, Expiration_Date__c  from Rental_Rate__C ';
    if (!Name.equals(''))
      soql += ' and Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
    if (!Account.equals(''))
      soql += ' and Account LIKE \''+String.escapeSingleQuotes(Account)+'%\'';
    if (!Active.equals(''))
      soql += ' and Active LIKE \''+String.escapeSingleQuotes(Active)+'%\'';
    if (!Rental_Group.equals(''))
      soql += ' and Rental_Group LIKE \''+String.escapeSingleQuotes(Rental_Group)+'%\'';  
    if (!Daily_Rate.equals(''))
      soql += ' and Daily_Rate LIKE \''+String.escapeSingleQuotes(Daily_Rate)+'%\'';
    if (!Weekly_Rate.equals(''))
      soql += ' and Weekly_Rate LIKE \''+String.escapeSingleQuotes(Weekly_Rate)+'%\'';
    if (!Monthly_Rate.equals(''))
      soql += ' and Monthly_Rate LIKE \''+String.escapeSingleQuotes(Monthly_Rate)+'%\'';
    if (!Expiration_Date.equals(''))
      soql += ' and Expiration_Date LIKE \''+String.escapeSingleQuotes(Expiration_Date)+'%\'';
     if (!Discounts.equals(''))
      soql += ' and Discount_Level__c includes (\''+Discounts+'\')';

    // run the query again
    runQuery();

    return null;
  }
 public List<String> discounts {
    get {
      if (discounts == null) {

        discounts = new List<String>();
        Schema.DescribeFieldResult field = Rental_Rate__c.Discount_Level__c.getDescribe();

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

      }
      return discounts;          
    }
    set;
  }
  

}


VF Page that is being embedded

 
<apex:page controller="RateSearchController" sidebar="false" showHeader="false">

  <apex:form >
  <apex:pageMessages id="errors" />
  <apex:pageBlock title="Find Rental Rates" 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("Account__c").value,
          document.getElementById("Active__c").value,
          document.getElementById("Daily_Rate__c").value,
          document.getElementById("Weekly_Rate__c").value,
          document.getElementById("Monthly_Rate__c").value,
          document.getElementById("Expiration_Date__c").value,
          document.getElementById("Discount_Level__c").value
          );
      }
      </script> 

      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="Name" value="" />
          <apex:param name="Account__c" value="" />
          <apex:param name="Rental_Group__c" value="" />
          <apex:param name="Discount_Level__c" 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;">Account<br/>
        <input type="text" id="Account" onkeyup="doSearch();"/>
        </td>
      </tr>
     
      <tr>
        <td style="font-weight:bold;">Rental Group<br/>
        <input type="text" id="Rental_Group" onkeyup="doSearch();"/>
        </td>
      </tr> 
      
      <tr>
        <td style="font-weight:bold;">Discount Level<br/>
          <select id="discounts" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!discounts}" var="disc">
              <option value="{!disc}">{!disc}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>     
     
     
      </table>
      
      

      </apex:pageBlock>

    </td>
    <td valign="top">

    <apex:pageBlock mode="edit" id="results">

        <apex:pageBlockTable value="{!rates}" var="RatesList">

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Rate Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!RatesList.Name}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Rental Group" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Rental_Group" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!RatesList.Rental_Group__c}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Account" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Account" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!RatesList.Account__c}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Discount Level" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="discounts" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!RatesList.Discount_Level__c}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Daily Rate" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Daily_Rate__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!RatesList.Daily_Rate__c}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Weekly Rate" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Weekly_Rate__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!RatesList.Weekly_Rate__c}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Monthly Rate" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Monthly_Rate__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!RatesList.Monthly_Rate__c}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Expiration Date" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Expiration_Date__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!RatesList.Expiration_Date__c}"/>
            </apex:column>




        </apex:pageBlockTable>

    </apex:pageBlock>

    </td>
  </tr>
  </table>

  
  </apex:pageBlock>

  </apex:form>

</apex:page>



 
Andy BoettcherAndy Boettcher
Your ActionFunction on line 28 is the actual call back to the Apex controller to run the search.  You are passing eight (8) variables in your Javascript search (line 15) but only have four (4) apex:params defined in the ActionFunction.

Try aligning both of those - without running the code myself, nothing else looks too out of whack beyond that.