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
vin_89vin_89 

unable to search the by name or by product

public with sharing class ProductSearchController
    {
    // the soql without the order and limit
     private String soql {get;set;}
     // the collection of products to display
    public List<Product2> Products {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 20'; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public ProductSearchController() {
    soql = 'select name,productcode from Product2 where 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 {
      Products = 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 productcode = Apexpages.currentPage().getParameters().get('productcode');
    soql = 'select name,productcode from product2 where name != null';
    if (!name.equals(''))
      soql += ' and name LIKE \''+String.escapeSingleQuotes(name)+'%\'';
    if (!productcode.equals(''))
      soql += ' and productcode LIKE \''+String.escapeSingleQuotes(productcode)+'%\'';
 // run the query again
    runQuery();
 
    return null;
  }

 

 

 

<apex:page controller="ProductSearchController" sidebar="false">
  <apex:form >
  <apex:pageMessages id="errors" />
  <apex:pageBlock title="Find Me A Product" 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("productcode").value,);
        }
      </script>         
          
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="name" value="" />
          <apex:param name="productcode" 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;">Product Code<br/>
        <input type="text" id="productcode" onkeyup="doSearch();"/>
        </td>
      </tr>
      </table>
 
      </apex:pageBlock>
      </td>
 
<td valign="top">
 
    <apex:pageBlock mode="edit" id="results">
 
        <apex:pageBlockTable value="{!Products}" var="Product">
 
            <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="{!Product.name}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="productcode" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="productcode" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!Product.productcode}"/>
            </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>

 

 

i am unable to search product either by name or by product code,i tried a lot but i am unable to find tghe solution,can anyone please go through the code and say what i am doing wrong in the code.

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

there is a little mistake in your java script code otherwise the whole code is good.

 

<script type="text/javascript">
        function doSearch()
        {
        searchServer
        (document.getElementById("name").value,
         document.getElementById("productcode").value );
        }
</script>  

 

Remove the comma at the end of the parameter which I have color as red ,This is the reason of uor problem......Please use inspect element to see the error of java script.

 

 

Please let me know if u have any problem on same and if this post hepls u please throw KUDOS by click on star at left.

All Answers

Puja_mfsiPuja_mfsi

Hi,

there is a little mistake in your java script code otherwise the whole code is good.

 

<script type="text/javascript">
        function doSearch()
        {
        searchServer
        (document.getElementById("name").value,
         document.getElementById("productcode").value );
        }
</script>  

 

Remove the comma at the end of the parameter which I have color as red ,This is the reason of uor problem......Please use inspect element to see the error of java script.

 

 

Please let me know if u have any problem on same and if this post hepls u please throw KUDOS by click on star at left.

This was selected as the best answer
Dhaval PanchalDhaval Panchal

Do below changes in your code (in red) or you can replace your code with my changed code also

 

Controller

 

public with sharing class ProductSearchController
    {
    // the soql without the order and limit
     private String soql {get;set;}
     // the collection of products to display
    public List<Product2> Products {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 20'; }
    set;
  }
 
  // init the controller and display some sample data when the page loads
  public ProductSearchController() {
    soql = 'select name,productcode from Product2 where 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 {
      Products = 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 string name{get;set;}
  public string productcode{get;set;}
  public PageReference runSearch() {
      //String name = Apexpages.currentPage().getParameters().get('name');
    //String productcode = Apexpages.currentPage().getParameters().get('productcode');
    soql = 'select name,productcode from product2 where name != null';
    if (!name.equals(''))
      soql += ' and name LIKE \''+String.escapeSingleQuotes(name)+'%\'';
    if (!productcode.equals(''))
      soql += ' and productcode LIKE \''+String.escapeSingleQuotes(productcode)+'%\'';
 // run the query again
    runQuery();
 
    return null;
  }

 
}
 

 VF Page:

 

<apex:page id="pg" controller="ProductSearchController" sidebar="false">
  <apex:form id="frm">
  <apex:pageMessages id="errors" />
  <apex:pageBlock id="pb" title="Find Me A Product" mode="edit">
  <table id="tbl" width="100%" border="0">
  <tr> 
    <td id="tdSearch" width="200" valign="top">
 
      <apex:pageBlock title="Parameters" mode="edit" id="criteria">
          
          
      <apex:actionFunction id="afSearchServer" name="searchServer" action="{!runSearch}" rerender="results,debug,errors"/>
    <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">Name<br/>
        <apex:inputText id="name" value="{!name}" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Product Code<br/>
        <apex:inputText id="productcode" value="{!productcode}" onkeyup="doSearch();"/>
        </td>
      </tr>
      </table>
 
      </apex:pageBlock>
      </td>
 
<td valign="top">
 
    <apex:pageBlock mode="edit" id="results">
 
        <apex:pageBlockTable value="{!Products}" var="Product">
 
            <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="{!Product.name}"/>
            </apex:column>
 
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="productcode" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="productcode" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!Product.productcode}"/>
            </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>
<!--I HAVE REMOVED YOUR JAVA SCRIPT FROM THE TOP AND ADDED BELOW SCRIPT-->
<script> function doSearch() { pg:frm:pb:criteria:afSearchServer:searchServer(); } </script> </apex:page>

I have tested and it works fine for me.

 

vin_89vin_89
it was a perfect solution.thanx a lot