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
john8407john8407 

VF Page and Controller to Search Products

I have a VF Page and Controller to run a search for products based on the product name and a custom field.  If I enter the product name and value for custom field in the controller query, it works.  I can't get it to reference what I enter in the fields on the VF page for the query.  Any help would be greatly appreciated.

 

Controller:

public with sharing class ProductSearchController2 {
   
    String strname = Apexpages.currentPage().getParameters().get('Name');
    String strcustom1= Apexpages.currentPage().getParameters().get('customfield');

 public ApexPages.StandardSetController setProduct {

        get {
            if(setProduct == null) {
                setProduct = new ApexPages.StandardSetController(Database.getQueryLocator([select Name, Customfield, MasterProduct__c, Rate__c from Product2 where Name = 'strname' AND customfield = 'strcustom1']));
            }
            return setProduct;
        }
        set;
    }
    public List<Product2> getProducts() {
         return (List<Product2>) setProduct.getRecords();
    }
}

 

VF Page:

<apex:page controller="ProductSearchController">
<script>
function runSearch()
{
   var Name = document.getElementById('txt1').value;
   var Customfield1 = document.getElementById('txt2').value;
}
       
function pressedKey()
{
if(event.keyCode==13)
{
document.getElementById("btnSearch").focus();
}
}
</script>
<apex:form id="generalsearch" onsubmit="runSearch();" onkeypress="pressedKey();">
<apex:pageBlock >
<b>Product Search: </b>
<input type="text" id="txt1" maxlength = "10" size = "5"/>
<input type="text" id="txt2" maxlength = "7" size = "7"/>
<input type="submit" value="Search" id="btnSearch" name="btnSearch"/>
</apex:pageBlock>
</apex:form>

    <apex:pageBlock >
        <apex:pageBlockTable value="{!Products}" var="p">
            <apex:column value="{!p.Name}"/>
            <apex:column value="{!p.Customfield1}"/>
            <apex:column value="{!p.MasterProduct__c}"/>
            <apex:column value="{!p.Rate__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

bob_buzzardbob_buzzard

If you change your <input> fields to <apex:inputText> and back them with a property from the controller, you can do without the javascript etc.  You are also using the literals strname and strcustom1 in your querylocator - you'll need to use a binding to those I think.

 

E.g.  controller

 

 

public with sharing class ProductSearchController2 {
    public String strname {get;set;}
    public String strcustom1 {get;set;}
 public ApexPages.StandardSetController setProduct {
        get {
            if(setProduct == null) {
                setProduct = new ApexPages.StandardSetController(Database.getQueryLocator([select Name, Customfield, MasterProduct__c, Rate__c from Product2 where Name = :strname AND customfield = :strcustom1]));
            }
            return setProduct;
        }
        set;
    }
    public List<Product2> getProducts() {
         return (List<Product2>) setProduct.getRecords();
    }
}
 

 

 

 

Page:

 

 

 

<apex:page controller="ProductSearchController">
<script>
function pressedKey() 
{
if(event.keyCode==13) 
{
document.getElementById("btnSearch").focus();
}
}
</script>
<apex:form id="generalsearch" onkeypress="pressedKey();">
<apex:pageBlock >
<b>Product Search: </b>
<apex:inputText id="txt1" maxlength = "10" size = "5" value="{!strname}"/>
<apex:inputText id="txt2" maxlength = "7" size = "7" value="{!strcustom1}"/>
<input type="submit" value="Search" id="btnSearch" name="btnSearch"/>
</apex:pageBlock>
</apex:form>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!Products}" var="p">
            <apex:column value="{!p.Name}"/>
            <apex:column value="{!p.Customfield1}"/>
            <apex:column value="{!p.MasterProduct__c}"/>
            <apex:column value="{!p.Rate__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>