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
Girbson Bijou 8Girbson Bijou 8 

Dynamic Search Visualforce

Hi All ,
I follow the Steps in the  link below to filter data but i do not see the Button to Fetch the data when i load the visualforce page. 
https://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/

Help me please
<apex:page controller="allItems" sidebar="false">

  <apex:form >
  <apex:pageMessages id="errors" />
  
  <apex:pageBlock title="Find Me A Customer!" 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("Product").value,
          document.getElementById("Comments").value,
          document.getElementById("Lot_Number").value,
          document.getElementById("centers").options[document.getElementById("technology").selectedIndex].value
          );
      }
      </script> 
      
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="Product" value="" />
          <apex:param name="lastName" value="" />
          <apex:param name="Lot_Number" value="" />
          <apex:param name="centers" value="" />
      </apex:actionFunction>
          
      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">Product Name<br/>
        <input type="text" id="Product" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Comments<br/>
        <input type="text" id="Comments" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Lot Number<br/>
        <input type="text" id="Lot_Number" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">FFP Centers<br/>
          <select id="centers" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!centerss}" var="tech">
              <option value="{!tech}">{!tech}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>
      
      </apex:pageBlock>
  
    </td>
    <td valign="top">
    
    <apex:pageBlock mode="edit" id="results">
                
        <apex:pageBlockTable value="{!allProduct}" var="allProd">
        
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Product Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Product_Hiden_Name__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!allProd.Product_Hiden_Name__c}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Comments" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Comments__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!allProd.Comments__c}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Lot_Number" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="Lot_Number__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!allProd.Lot_Number__c}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="centers" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="FFP_Centers__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!allProd.FFP_Centers__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>

Controller:   I get the following  in the  lines 61, 63, 65 when i use the quote ' ' in the Original post.
Compile Error: Found punctuation symbol or operator '%' that isn't valid in Apex. at line 61 column 85
public with sharing class allItems {
  
  // the soql without the order and limit
  private String soql {get;set;}
  // the collection of contacts to display
  public List<Articles_Containers__c> allProduct {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 = '    FFP_Centers__c'; } 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 allItems() {
    soql = 'select Name ,Left_Warehouse_and_Delivered__c, Lot_Number__c, Expiration_Date__c, Product_Hiden_Name__c,FFP_Centers__c,Prod__c,UM__c, Container__c, Number__c, On_Hand__c,  Pending__c,  Available__c, Purpose__c, Condition__c, Age__c, Comments__c , Prod__c FROM Articles_Containers__c WHERE On_Hand__c >0  AND IsOpened__c = 1 ORDER BY FFP_Centers__c, Prod__c,UM__c ,Number__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 {
      allProduct = 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 Product = Apexpages.currentPage().getParameters().get('Product_Hiden_Name__c');
    String Comments = Apexpages.currentPage().getParameters().get('Comments__c');
   String Lot_Number = Apexpages.currentPage().getParameters().get('Lot_Number__c');
    String centers = Apexpages.currentPage().getParameters().get('FFP_Centers__c');
    
    soql = 'select Name ,Left_Warehouse_and_Delivered__c, Lot_Number__c, Expiration_Date__c, Product_Hiden_Name__c,FFP_Centers__c,Prod__c,UM__c, Container__c, Number__c, On_Hand__c,  Pending__c,  Available__c, Purpose__c, Condition__c, Age__c, Comments__c , Prod__c FROM Articles_Containers__c WHERE On_Hand__c >0  AND IsOpened__c = 1 ORDER BY FFP_Centers__c, Prod__c,UM__c ,Number__c';
   if (!Product.equals(''))
    soql += ' and Product LIKE "+String.escapeSingleQuotes(Product_Hiden_Name__c)+"%';
   if (!Comments.equals(''))
   soql += ' and Comments__c LIKE "+String.escapeSingleQuotes(Comments__c)+"%';
   if (!Lot_Number.equals(''))
     soql += 'and Lot_Number__c LIKE "+String.escapeSingleQuotes(Lot_Number__c)+"%';  
    if (!centers.equals(''))
      soql += 'and FFP_Centers__c includes ("+centers+")';

    // run the query again
    runQuery();

    return null;
  }
  
  // use apex describe to build the picklist values
  public List<String> centerss {
    get {
      if (centerss == null) {
              
        centerss = new List<String>();
        Schema.DescribeFieldResult field = Articles_Containers__c.FFP_Centers__c.getDescribe();
            
        for (Schema.PicklistEntry f : field.getPicklistValues())
          centerss.add(f.getLabel());
          
      }
      return centerss;          
    }
    set;
  }

}

​​​​​​​
Deepali KulshresthaDeepali Kulshrestha
Hi Girbson,

You can implement the dynamic search functionality through VisualForce  Page as follows. Please refer to the following 

code as it may be helpful in solving your issues:
 
Controller:

public with sharing class AccountMultipleSearchWithPagenationCLS {

public Account acc{get;set;}

public List<Account> accountList {get;set;}

// create a list of strings to hold the conditions

List<string> conditions = new List<string>();

private integer totalRecs = 0;

private integer OffsetSize = 0;

private integer LimitSize= 10;


public AccountMultipleSearchWithPagenationCLS(){

system.debug('==>AccountMultipleSearchWithPagenationCLS  is calling==>');
 
acc = new Account();

 //accountList  = new List<Account>();

}


public void searchAcc(){

totalRecs = 0;

OffsetSize = 0;

if(accountList !=null && accountList.size()>0){
 
accountList=null;

}

searchAccounts ();

conditions.clear();

}



public Void searchAccounts(){


System.debug('Total Records is ==>'+totalRecs);

System.debug('OffsetSize is ==>'+OffsetSize);


if(accountList != null && !accountList.isEmpty()){
 
 accountList.clear();

}
 
String strQuery ='SELECT Id,Name,AccountNumber,CreatedDate,Phone,Website,Industry,AnnualRevenue From Account';
 

 if(acc.Name !=null && acc.Name !=''){
 
  conditions.add('Name Like \'%' +acc.Name +'%\' ');
 
}
 
 if(acc.AccountNumber !=null && acc.AccountNumber !=''){

   conditions.add('AccountNumber Like\'%' +acc.AccountNumber +'%\' ');

 }


  if (conditions.size() > 0) {
 
  strQuery += '  WHERE ' + conditions[0];

   for (Integer i = 1; i < conditions.size(); i++)
      
      strQuery += '  AND ' + conditions[i];
  
}
 
if(totalRecs !=null && totalRecs ==0){
 
   List<Account> accTemp = Database.query(strQuery);

    totalRecs = (accTemp !=null &&accTemp.size()>0)?accTemp.size():0;
 
}


 system.debug('strQuery ==>'+strQuery );
 
// add sort and limits at the end  

  strQuery += ' ORDER BY Name  ASC, CreatedDate DESC LIMIT :LimitSize OFFSET :OffsetSize';
  

  accountList  =Database.query(strQuery);
  

   

  //conditions.clear();
 
 //return accountList.size();

}


public void FirstPage()
{

OffsetSize = 0;

searchAccounts();

}

public void previous()
{

OffsetSize = (OffsetSize-LimitSize);

searchAccounts();

}

public void next()
{

OffsetSize = OffsetSize + LimitSize;

searchAccounts();

}

public void LastPage()
{

OffsetSize = totalrecs - math.mod(totalRecs,LimitSize);

searchAccounts();

}

public boolean getprev()
{


if(OffsetSize == 0){


return true;

}
else {


return false;

}

}

public boolean getnxt()
{

if((OffsetSize + LimitSize) > totalRecs){


return true;

}
else {


return false;

}

}



}


VF Page:

<apex:page controller="AccountMultipleSearchWithPagenationCLS" action="{!searchAcc}" >

<script type="text/javascript">
 
   window.onload=function() {
  
  // document.getElementById("{!$Component.thePb.thepbs.accName}").focus();
 
   } 
  
</script>

 <apex:form >
  
<apex:pageBlock id="thePb" title="Account Details To Search">
  
 <apex:pageblockSection id="thepbs">
  
 <apex:inputField value="{!acc.Name}" required="false" id="accName"/>
   
  <apex:inputfield value="{!acc.accountNumber}"/>
 
  </apex:pageblockSection>
 
  <apex:pageblockButtons location="bottom">
  
    <apex:commandButton value="Search" action="{!searchAcc}" />
 
    </apex:pageblockButtons>  

  </apex:pageBlock>
  

   <apex:pageBlock title="Account Details" id="noRec" rendered="{! IF( accountList != null && accountList.size ==0 , true, 

false)}" >

  <apex:outputPanel >
  
  <h1>No Records Found </h1>

</apex:outputPanel>

  </apex:pageBlock>

 
 
  <apex:pageBlock title="Account Details" id="details" rendered="{! IF( accountList != null && accountList.size >0, true, 

false)}" >

 
  <apex:pageBlockTable value="{!accountList}" var="a">
 
  <apex:column headerValue="Account Name">
 
   <apex:outputLink target="_blank" value="/{!a.id}">{!a.Name}</apex:outputLink> 
  
 </apex:column>   

    <!--  If you want facet style you can add like this.
   <apex:column >
     <apex:facet name="header">Link 

Name</apex:facet>
  
   <apex:outputLink target="_blank" value="/{!a.id}">{!a.Name}</apex:outputLink>
 
    </apex:column>
    -->
  
  <apex:column value="{!a.accountNumber}" headerValue="Account Number"/>  

    <apex:column value="{!a.Industry}" headerValue="Industry"/> 
 
    <apex:column value="{!a.AnnualRevenue}" headerValue="Annual Revenue"/> 
 
  <apex:column value="{!a.Phone}" headerValue="Phone"/>   

  <apex:column value="{!a.website}" headerValue="Web"/>   
 
   </apex:pageBlockTable>
   

    <apex:pageblockButtons >
 
<apex:commandButton value="First Page" rerender="details" action="{!FirstPage}" disabled="{!prev}"/>

<apex:commandButton value="Previous" rerender="details" action="{!previous}" disabled="{!prev}"/>

<apex:commandButton value="Next" rerender="details" action="{!next}" disabled="{!nxt}"/>

<apex:commandButton value="Last Page" rerender="details" action="{!LastPage}" disabled="{!nxt}"/>

</apex:pageblockButtons>

   
  </apex:pageBlock>


 </apex:form>

</apex:page>

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha