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
Aron Schor [Dev]Aron Schor [Dev] 

Help with VF search page ("line breaks not allowed" error) and other questions

Hi, I can use the code here, but I want to tweak it to work for Products.  I am hoping with some basic changes it can work.  I am a bit new to programming.  I posted on the site asking for help but got no reply so hoping someone here can help.
http://nirmalchristopher.blogspot.com/2013/12/search-page-in-visualforce-simplified.html

I tried changing the query to be
     string searchquery='select Name, ProductCode, Description, Notes__C from Product2 where (Name like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Description like \'%'+searchstring+'%\' OR Notes__C like \'%'+searchstring+'%\');

and it says
    Error: Compile Error: line breaks not allowed in string literals at line 7 column -1

How do I fix?

Also, I assume to modify the page I would have to change 

1. <apex:page standardController="account" extensions="accsearchcontroller">  
"account" to "Product2"

2. and also change add output.
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.ProductCode}</apex:outputlink>  
etc

Anything else?

Thanks!
Best Answer chosen by Aron Schor [Dev]
KaranrajKaranraj
Can you share your Apex code also? Do you have variable with name 'pro'  in the apex class? To which variable you are storing the Database.query result, you have to use that variable  in that place. 

All Answers

KaranrajKaranraj
Aron - You dind't close the single quotes properly in your code, that is the reason for the query. Try with the below line
 
string searchquery='select Name, ProductCode, Description, Notes__C from Product2 where Name like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Description like \'%'+searchstring+'%\' OR Notes__C like \'%'+searchstring+'%\'';
You don't need to make any other change in the code.
 
Aron Schor [Dev]Aron Schor [Dev]
Thanks Karanraj,

The Apex class seems to work!

How about the page?  I guess the issue is line one.

It says:
Error: Unknown property 'Product2StandardController.pro'    

I guessed and tried CustomController.
Error: Unsupported attribute customcontroller in <apex:page> in prodsearch at line 1 column 74    

Just controller says:
Error: Apex class 'Product2' does not exist

 <apex:page standardController="Product2" extensions="prodsearchcontroller">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!pro}" var="a">  
     <apex:column >  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.ProductCode}</apex:outputlink>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Description}</apex:outputlink>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Notes__C}</apex:outputlink>  
     </apex:column>  
     <apex:column value="{!a.id}"/>  
    </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page> 


Notes regarding the object, if it helps.
label: Product
labelPlural: Products
name: Product2
KaranrajKaranraj
Can you share your Apex code also? Do you have variable with name 'pro'  in the apex class? To which variable you are storing the Database.query result, you have to use that variable  in that place. 
This was selected as the best answer
Aron Schor [Dev]Aron Schor [Dev]
Almost, its just a display issue now! (I fixed the issues there)

Here is the page code and what I see.  I want columns for the different values and I don't need the Product id part.

Results

<apex:page standardController="Product2" extensions="prodsearchcontroller">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!prod}" var="a">  
     <apex:column >  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.ProductCode}</apex:outputlink>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Description}</apex:outputlink>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Notes__c}</apex:outputlink>  
     </apex:column>  
     <apex:column value="{!a.id}"/>  
    </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>

______________

Apex Class

public with sharing class prodsearchcontroller {  
   public list <Product2> Prod {get;set;}  
   public string searchstring {get;set;}  
   public prodsearchcontroller(ApexPages.StandardController controller) {  
   }  
   public void search(){  
     string searchquery='select Name, ProductCode, Description, Notes__C from Product2 where Name like \'%'+searchstring+'%\' OR ProductCode like \'%'+searchstring+'%\' OR Description like \'%'+searchstring+'%\' OR Notes__C like \'%'+searchstring+'%\'';
     Prod= Database.query(searchquery);  
   }  
   public void clear(){  
   prod.clear();  
   }  
 }

_______________

I tried using this code but it says (just a guess on my part.  Not sure how to fix)
    Error: <apex:column> must be the direct child of either <apex:dataTable> or <apex:pageBlockTable>

<apex:page standardController="Product2" extensions="prodsearchcontroller">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!prod}" var="a">  
     <apex:column >  
     <apex:column value="{!a.Name}"/>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>  
     <apex:column >  
     <apex:column value="{!a.ProductCode}"/>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.ProductCode}</apex:outputlink>  
     <apex:column >  
     <apex:column value="{!a.Description}"/>  
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Description}</apex:outputlink>  
     <apex:column >  
     <apex:column value="{!a.Notes__C}"/> 
      <apex:outputlink value="https://ap1.salesforce.com/{!a.id}">{!a.Notes__c}</apex:outputlink>  
     </apex:column>  
          </apex:column>  
               </apex:column>  
                    </apex:column>  
    </apex:pageBlockTable>     
   </apex:pageBlock>   
  </apex:form>  
 </apex:page>

Thanks