You need to sign in to do that
Don't have an account?
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.
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
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.
Do below changes in your code (in red) or you can replace your code with my changed code also
Controller
VF Page:
I have tested and it works fine for me.