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
Harshal Katole 7Harshal Katole 7 

searching record dynamicaly

i have course field on Training_Deal__c
==============================
public class SearchCourse {
    public string SearchText {set;get;}
     public string query {set;get;}
     public List<Training_Deal__c> deal {set;get;}
    public void fetchData(){
       
        string query = 'SELECT id, Name, Course__c,Discount__c,Student__c,Email_of_Student__c,Trainer_Appointed__c FROM Training_Deal__c WHERE Course__c =:'+SearchText ;
        deal=Database.query(query);
        system.debug(deal);
    }

}
=======================================
<apex:page controller="SearchCourse" >
    <apex:form id="fm">
        <apex:inputText value="{!SearchText}"/>
        <apex:commandButton value="Search" action="{!fetchData}" reRender="fm"/><br/><br/>
        <apex:dataTable value="{!deal}" var="a" cellpadding="5"  rules="Rows" width="20" border="frame">
            <apex:column value="{!a.Name}" headerValue="Deal code"/>
            <apex:column value="{!a.Course__c}" headerValue="Course"/>
            <apex:column value="{!a.Discount__c}" headerValue="Discount"/>
            <apex:column value="{!a.Student__c}" headerValue="Student"/>
            <apex:column value="{!a.Email_of_Student__c}" headerValue="Student Email"/>
            <apex:column value="{!a.Trainer_Appointed__c}" headerValue="Trainer"/>
  
                         
        </apex:dataTable>
    </apex:form>
</apex:page>
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please check the below link and follow the code

https://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/

<apex:page controller="ContactSearchController" sidebar="false">

  <apex:form >
  <apex:pageMessages id="errors" />
  
  <apex:pageBlock title="Find Me A Customer!" mode="edit">
    
  <table width="100%" border="0">
  <tr>  
    <td width="200" valign="top">
  
      <apex:pageBlock title="Parameters" mode="edit" id="criteria">
      
      <script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("firstName").value,
          document.getElementById("lastName").value,
          document.getElementById("accountName").value,
          document.getElementById("technology").options[document.getElementById("technology").selectedIndex].value
          );
      }
      </script> 
      
      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="firstName" value="" />
          <apex:param name="lastName" value="" />
          <apex:param name="accountName" value="" />
          <apex:param name="technology" value="" />
      </apex:actionFunction>
          
      <table cellpadding="2" cellspacing="2">
      <tr>
        <td style="font-weight:bold;">First Name<br/>
        <input type="text" id="firstName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Last Name<br/>
        <input type="text" id="lastName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Account<br/>
        <input type="text" id="accountName" onkeyup="doSearch();"/>
        </td>
      </tr>
      <tr>
        <td style="font-weight:bold;">Interested Technologies<br/>
          <select id="technology" onchange="doSearch();">
            <option value=""></option>
            <apex:repeat value="{!technologies}" var="tech">
              <option value="{!tech}">{!tech}</option>
            </apex:repeat>
          </select>
        </td>
      </tr>
      </table>
      
      </apex:pageBlock>
  
    </td>
    <td valign="top">
    
    <apex:pageBlock mode="edit" id="results">
                
        <apex:pageBlockTable value="{!contacts}" var="contact">
        
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="First Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="firstName" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.firstName}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Last Name" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="lastName" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.lastName}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Account" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="account.name" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.account.name}"/>
            </apex:column>
            
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Technologies" action="{!toggleSort}" rerender="results,debug">
                        <apex:param name="sortField" value="interested_technologies__c" assignTo="{!sortField}"/>
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!contact.Interested_Technologies__c}"/>
            </apex:column>
            
        </apex:pageBlockTable>
                
    </apex:pageBlock>
    
    </td>
  </tr>
  </table>
  
  <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />           
  </apex:pageBlock>    
  
  </apex:pageBlock>

  </apex:form>

</apex:page>

https://sfdcfanboy.com/2016/06/14/visualforce-pagination-with-dynamic-search/

https://salesforce.stackexchange.com/questions/25879/dynamic-search-using-visualforce-page

Please mark it as the Best If it helps you 

Thank You

Maharajan CMaharajan C
Hi Harshal,

Please change the query like below then try it:
 
string query = 'SELECT id, Name, Course__c,Discount__c,Student__c,Email_of_Student__c,Trainer_Appointed__c FROM Training_Deal__c WHERE Course__c = \'' + SearchText + '\'' ;

Thanks,
Maharajan.C