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
Eric Blaxton.ax1862Eric Blaxton.ax1862 

I want to create a "Product" search button that opens a custom VF page

Thanks in advance.

Requirement is to have a text field or drop down list that the users enters/chooses a product and then clicks search.  The search button then displays all of the opps and quotes where this product is listed.

What would be the best way to achieve this?

Regards,
PeaceMakerPeaceMaker
Hi Eric,

You can use the below code for your requirement, it will work fine for all objects , you just need to add objects to the List.

Just create a new VF Page and copy paste the code. it will work fine.

VF Page:

<apex:page controller="searchrecords">
  <apex:form >

   <apex:PageBlock title="">
    <center>
          <b> Enter text to search: </b> &nbsp;&nbsp;<apex:inputText Title="Select Type" value="{!searchtext}" /> &nbsp;&nbsp;&nbsp;
            <apex:commandButton id="searchStatus" value="Search" action="{!search}"/>&nbsp;&nbsp;   <apex:commandButton value="Clear Filter" action="{!clearFilter}" />&nbsp;&nbsp;<!--<a href="javascript:showhide('hideShow')">Advanced Search</a> --> 
    &nbsp;&nbsp;&nbsp;    <apex:Messages style="color:rgb(204,0,0);font-weight: bold;text-align:center" /> &nbsp;&nbsp;&nbsp; <apex:outputText value="{!dispMessage} " style="font-weight:bold;Font-size:12px;color:Green;"></apex:outputText></center><br/><br/><br/>
  
         <apex:pageBlockTable value="{!searchResults}" rendered="{!searchResults.size > 0}" var="Usr" style="width:100%; height:12px;" rows="1500">
                 <apex:column headerClass="">
                        <apex:facet name="header">Object</apex:facet>
                         <apex:outputText value="{!Usr.sObjectTypeName}"/>
                </apex:column>
          <apex:column headerClass="">
                        <apex:facet name="header">Name</apex:facet>
                         <apex:outputLink Value="/{!Usr.record['Id']}" >
                         <apex:outputText value="{!Usr.record['Name']}"/></apex:outputLink>
                </apex:column>
                <apex:column headerClass="">
                        <apex:facet name="header">Created By</apex:facet>
                         <apex:outputLink Value="/{!Usr.record['CreatedbyId']}" >
                         <apex:outputText value="{!Usr.record['Createdby.Name']}"/></apex:outputLink>
                </apex:column>
                 <apex:column headerClass="">
                        <apex:facet name="header">Created date</apex:facet>
                         <apex:outputText value="{!Usr.record['Createddate']}"/>
                </apex:column>
                <apex:column headerClass="">
                        <apex:facet name="header">Last Modified By</apex:facet>
                         <apex:outputLink Value="/{!Usr.record['LastmodifiedbyId']}" >
                         <apex:outputText value="{!Usr.record['Lastmodifiedby.Name']}"/></apex:outputLink>
                </apex:column>
                 <apex:column headerClass="">
                        <apex:facet name="header">Last Modified date</apex:facet>
                         <apex:outputText value="{!Usr.record['Lastmodifieddate']}"/>
                </apex:column>
</apex:pageBlockTable>


     </apex:PageBlock>
</apex:form>

</apex:page>


Class :

public with sharing class searchrecords {


public string searchtext{get; set;}
public string dispMessage{get; set;}
public list<list<sobject>> searchList{get; set;}
Public List<String> searchObjList{get;set;}
Public string searchquery{get; set;}
public string Objectstr{get; set;}
public string dispObjectstr{get; set;}

public searchrecords()
{
    searchObjList = new List<String>();
    Objectstr = '';
    dispObjectstr = '';
 
}


public pagereference search(){
    if(searchtext != null && searchtext.length() > 1){
        Objectstr = '';
        dispObjectstr = '';
    // Construct the search SOSL Query
      list<string> gd = new List<string>();
      gd.add('Opportunity');
      //gd.add('Quote');
     
       
            for(String s:gd){
              
                    Objectstr += s+'(id,Name,createdby.Name,lastmodifiedby.Name,createdbyId,lastmodifiedbyId,CreatedDate,lastmodifieddate),\n';
                    dispObjectstr  += s+'\n';
                   
               
            }
            system.debug('Objectstr---->'+Objectstr);
            Objectstr = Objectstr.substring(0,Objectstr.length()-2);
            searchquery='FIND \'' +searchtext+'*\' IN ALL FIELDS RETURNING '+Objectstr;
            searchList = new List<List<SObject>>();
            try{
                    searchList = search.query(searchquery);
                    if(searchResults.size() == 1)
                    dispMessage = searchResults.size()+' Record found!';
                    else
                    dispMessage = searchResults.size()+' Records found!';
                }
            catch(system.queryException ex)
            {
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.Warning,'Database Query error...'+ex));
            }
       
       
    }
    else
    {
         ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.Warning,'Please enter minimum two characters to search.'));
    }
   
   
    return null;
}


public class searchResultRow{
        public string sObjectTypeName {get;set;}
        public sObject record {get;set;}
       
        public searchResultRow(sObject pObject) {
            record = pObject;
            sObjectTypeName = pObject.getSObjectType().getDescribe().getLabel();
        }
    } 
   
    public list<searchResultRow> searchResults {
        get {
            searchResults = new list<searchResultRow>();
            if(searchList != null) {
                for(list<sObject> objectList : searchList) {
                    for(sObject obj : objectList) {
                        searchResults.add(new searchResultRow(obj));
                    }
                }
            }
            return searchResults;
        }
        set;
    }
   


   
    public void clearFilter(){
        searchtext ='';
        dispMessage = '';
    }

}



For Some reason Quote is not supporting..  you can modify the code as per your needs.

If this code helps you , please mark this as solution.

PeaceMakerPeaceMaker
Note:  I have used SOSL query to search..
AndrewFisher_TGPAndrewFisher_TGP
Does anyone have a test class for this ? It would greatly appreciated