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
sfdc dev 2317sfdc dev 2317 

Create lookup field on VF page + dynamic search based on lookup field value

Hello,
I have VF page, build to perform search on Case object. I am already performing search based on Case Status, Case Number.
I want to add one more search based on field "Product". Product(Master) is a lookup field on Case(Child). 
So, how do I create a Product lookup field (with magnifying symbol besides it) and Add that its values in soql and perform a search? 
Any help is appriciated !!
Thanks in advance.

Visualforce page:
apex:page controller="VF_CaseSearch" action="{!searchCase}" tabStyle="Case" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Case Details To Search">
            <apex:pageblockSection >
                <apex:inputText value="{!cas.CaseNumber}" label="Search Case Number"/>
                <apex:inputCheckbox value="{!cas.Status_Closed__c }" label="Status Closed?"/>
                <apex:inputText value="{!cas.Product__c}" label="Search Product"/>
            </apex:pageblockSection>

 <!---Search Button--> 
            <apex:pageblockButtons location="bottom">
                <apex:commandButton value="Search" action="{!searchCase}"/>
            </apex:pageblockButtons>
       </apex:pageBlock>
 <apex:pageBlock>
  <apex:pageBlockTable value="{!caseList}" var="c"> 
                <apex:column value="{!c.CaseNumber}" headerValue="Case Number"/> 
               <apex:column value="{!c.Status}" headerValue="Status"/>
                <apex:column value="{!c.Product__c}" headerValue="Product"/>
            </apex:pageBlockTable>
      </apex:pageBlock>
   </apex:form>
</apex:page>

Custom Controller:
public with sharing class VF_CaseSearch {

  public Case cas{get;set;}
  public List<Case> caseList {get;set;}
  List<string> conditions = new List<string>();
  
    public VFC_CasesMultipleSearch(){
      cas = new Case();
  }
  
  public void searchCase(){
      if(caseList !=null && caseList.size()>0){
          caseList=null;
      }
      searchCases ();
      conditions.clear();
  }
  
  
  public Void searchCases(){
      if(caseList != null && !caseList.isEmpty()){
          caseList.clear();
      }

      //create a dynamic query for filter results
      String strQuery ='SELECT Id, CaseNumber, Product__c, Status, Status_Closed__c FROM Case';
    
      if(cas.CaseNumber !=null && cas.CaseNumber !=''){
          conditions.add('CaseNumber Like\'%' +cas.CaseNumber +'%\' ');
      }
      
       if(cas.Product__c !=null && cas.Product__c !=''){
          conditions.add('Product__r.Name Like\'%' +cas.Product__c +'%\' ');
           system.debug('Product Name' + cas.Product__c);
      }
            
       if(cas.Status_Closed__c){
          conditions.add('Status_Closed__c='+cas.Status_Closed__c);
       }else{
           conditions.add('Status_Closed__c='+ cas.Status_Closed__c);
       }
      
      if (conditions.size() > 0) {
          strQuery += '  WHERE  ' + conditions[0];
          for (Integer i = 1; i < conditions.size(); i++)
              strQuery += '  AND  ' + conditions[i];
      }
      
      caseList = Database.query(strQuery);

  }
}



 
Best Answer chosen by sfdc dev 2317
sfdc dev 2317sfdc dev 2317
Here is the solution suggested from https://salesforce.stackexchange.com/questions (https://salesforce.stackexchange.com/questions/313930/create-lookup-field-on-vf-page-dynamic-search-based-on-lookup-field-value)

Modify Visualforce page Line 07: from inputText to inputField
<apex:inputField value="{!cas.Product__c}" label="Search Product"/>
Also modify query in Custom Controller line 32 and 33
if(!String.isEmpty(cas.Product__c)){ 
conditions.add('Product__c =\'' +cas.Product__c +'\' ');
}