+ Start a Discussion
OldDeadBugOldDeadBug 

Basic VisualForce Question

I know this is newbie stuff, but I don't know how else to learn but to ask stupid questions:

I have a ProductResult Class constructor which is a placeholder for results of a Product Query.

I have a ProdResController with the following code:

public class ProdResultController {

Product2[] prodQry;
ProductResult[] prodRes = new ProductResult[]{};
string SearchString;

public ProductResult[] getProductSearch()
{

prodQry = [SELECT id, Name, ProductCode, Description, Family, isActive, SVMXI__Product_Cost__c
FROM Product2
WHERE Name like :SearchString
OR Description like :SearchString
OR ProductCode = :SearchString];

if (prodQry.size() > 0)

for (Product2 prd :prodQry)
{
ProductResult newPR = new ProductResult(prd.id, prd.Name, prd.ProductCode, prd.Description, prd.Family,
prd.isActive, prd.SVMXI__Product_Cost__c);
prodRes.add(newPR);
}

return prodRes;
}
}


What I want to do is create a form page with an input field for a SearchString and a button that calls the getProductSearch function to query the Products based on the input string, and create a ProductResult record based on the results, and return it to the page somewhere that hasn't been created yet.

The page code, without the tags (as it doesn't seem to display in the preview) is:

apex:page controller="ProdResultController">
apex:form >
apex:pageBlock title="Product Search">
p/>
Search for: apex:inputField value="{!SearchString}"/>p/>
apex:commandButton action="{!ProductSearch}" value="Search for Products"/>

/apex:pageBlock>
/apex:form>

/apex:page>


The Save Error I'm receiving is "Could not resolve the entity from apex:inputField> value binding {!SearchString}. inputField can only be used with sObject fields"

I'm sure this message is clear as day to most of you, but I'm not sure where to begin. Do I have to define SearchString in the ProductResult constructor class, even though its not really part of the result, but just a string used to get a query?
Do I need to define Searchstring inside the getProductSearch function, or should it not be referenced by the controller definition in the page?
Does the Product Search function require a "get" in this case, or am I not "getting" the 'get' part of a controller function?

How many other completely braindead mistakes am I making here?

Any help would be appreciated.

VM
Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

It's much more than just that.  I think you really need to take a swing through the examples in the dev guide.

 

inputField can only be used with SObjects, so if you're going to be binding to a string property in your controller you should be using inputText.

 

Read up on action methods and how to use them.   Your commandButton's action attribute must be bound to an action method.  So either a void method or a method that returns PageReference.  In your example you probably want the former.  Instead of returning the results, stash them in a variable in your controller, and then use dataTable or pageBlockTable or repeat to iterate over the results you get back.

 

You're attempting a pretty simple use case and you're on the right track.  I think you'd get a lot of mileage from the examples.  Right now things just aren't wired up quite right. 

All Answers

ryan_marplesryan_marples
Declaring a variable at the controller level isn't enough to expose it to your visualforce page. It needs to have a public getter. The easiet way to declare it is to use the default getter and setter implementation. So it would look like this:

public String SearchString { get; set; }

Ryan
jwetzlerjwetzler

It's much more than just that.  I think you really need to take a swing through the examples in the dev guide.

 

inputField can only be used with SObjects, so if you're going to be binding to a string property in your controller you should be using inputText.

 

Read up on action methods and how to use them.   Your commandButton's action attribute must be bound to an action method.  So either a void method or a method that returns PageReference.  In your example you probably want the former.  Instead of returning the results, stash them in a variable in your controller, and then use dataTable or pageBlockTable or repeat to iterate over the results you get back.

 

You're attempting a pretty simple use case and you're on the right track.  I think you'd get a lot of mileage from the examples.  Right now things just aren't wired up quite right. 

This was selected as the best answer