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
SainSain 

Facing problem with fetching data based on given search

Hi,
I have custom object called patient and fields name, email__c, gender__c, Doctor__c(lookup field).
I have to develop a dynamic search page to fetch the related data and display in a table based on values given in input fields called name, email__c, Doctor__c.  but iam facing problem with fetching data based on search.

please help me to solve this issue.

<apex:page controller="DynamicSearchPatientClass" sidebar="false">
<script type="text/javascript">

      function doSearch() {

        searchServer(
          document.getElementById("Name").value,
          document.getElementById("Gender").value,
          document.getElementById("Email").value,
          document.getElementById("Doctor").value);
      }
      </script>

<apex:form >

<apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,result2,errors">
          <apex:param name="Name" value="" />
          <apex:param name="Gender" value="" />
          <apex:param name="Email" value="" />
          <apex:param name="Doctor" value="" />
</apex:actionFunction>

<apex:pageBlock >
<apex:pageBlockSection title="Patient Search" collapsible="false" columns="2" >
Patient Name<apex:inputText value="{!patients.Name}" id="Name" onkeyup="doSearch();"/>
Gender<apex:inputText value="{!patients.Gender__c}" id="Gender" onchange="doSearch();"/>
Email<apex:inputText value="{!patients.Email__c}" id="Email" onkeyup="doSearch();"/>
Doctor Name<apex:inputText value="{!patients.Doctor__c }" id="Doctor" onchange="doSearch();"/>
</apex:pageBlockSection>

<apex:pageBlockSection title="Patient Details" id="result" >
<apex:pageBlockTable value="{!patients}" var="p" id="result2">
<apex:column value="{!p.Name}"/>
<apex:column value="{!p.Gender__c}"/>
<apex:column value="{!p.Email__c}"/>
<apex:column value="{!p.Doctor__c }"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Class:
public with sharing class DynamicSearchPatientClass {

Private string soql {get;set;}
public Patient__c patients {get;set;}

public String sortDir{
      get { if (sortDir == null) { sortDir = 'asc'; }
                   return sortDir;}
      set;}

public String sortField {
          get {if  ( sortField == null) {
                  sortField = 'Name'; } return sortField; }
          set;}

public void runQuery() {
              try {
                   patients= Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20 ');
                   system.debug('>>>>>>4' +patients);
                    }
                    catch (Exception e) {
                     ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ' OOOps! ') );
                     }
     }    

public DynamicSearchPatientClass(){
 soql='select name,Email__c,Gender__c,Doctor__c from patient__c where patient__c.Name != null';
 system.debug('>>>>>>' +soql);
runQuery();

}

public PageReference runSearch(){
    String Name = Apexpages.currentPage().getParameters().get('Name');
    String Gender = Apexpages.currentPage().getParameters().get('Gender');
    String Email = Apexpages.currentPage().getParameters().get('Email');
    String Doctor= Apexpages.currentPage().getParameters().get('Doctor');

    soql='select name,Email__c,Gender__c,Doctor__c from patient__c where patient__c.Name != null';   
system.debug('>>>>>>3' +soql);
    if (!Name.equals(''))
      soql += ' and Name LIKE \' '+String.escapeSingleQuotes(Name)+'%\'';
   system.debug('>>>>>>1' +soql);
    if (!Email.equals(''))
      soql += ' and Email__c LIKE \''+String.escapeSingleQuotes(Email)+'%\'';
     if (!Gender.equals(''))
     system.debug('>>>>>>2' +soql);
      soql += ' and Gender__c LIKE \''+String.escapeSingleQuotes(Gender)+'%\'';
    if (!Doctor.equals(''))
      soql += ' and Doctor__c LIKE \''+String.escapeSingleQuotes(Doctor)+'%\'';

    runQuery();

return null;

}
}


Regards,
Sain
 
hitesh90hitesh90
Hello Sain,

Try to use follwing code.

Visualforce Page:
<apex:page id="myPage" controller="DynamicSearchPatientClass" sidebar="false">
    <script type="text/javascript">
    function doSearch() {
        searchServer(
            document.getElementById("myPage:myFrm:pbMain:pbsMain:Name").value,
            document.getElementById("myPage:myFrm:pbMain:pbsMain:Gender").value,
            document.getElementById("myPage:myFrm:pbMain:pbsMain:Email").value,
            document.getElementById("myPage:myFrm:pbMain:pbsMain:Doctor").value);
    }
    </script>    
    <apex:form id="myFrm">
        <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="result">
            <apex:param name="Name" value="" />
            <apex:param name="Gender" value="" />
            <apex:param name="Email" value="" />
            <apex:param name="Doctor" value="" />
        </apex:actionFunction>       
        <apex:pageBlock id="pbMain">
            <apex:pageBlockSection id="pbsMain" title="Patient Search" collapsible="false" columns="2" >
                Patient Name<apex:inputText value="{!objPatients.Name}" id="Name" onkeyup="doSearch();"/>
                Gender<apex:inputText value="{!objPatients.Gender__c}" id="Gender" onchange="doSearch();"/>
                Email<apex:inputText value="{!objPatients.Email__c}" id="Email" onkeyup="doSearch();"/>
                Doctor Name<apex:inputText value="{!objPatients.Doctor__c }" id="Doctor" onchange="doSearch();"/>
            </apex:pageBlockSection>            
            <apex:pageBlockSection title="Patient Details" id="result" >
                <apex:pageBlockTable value="{!patients}" var="p" id="result2">
                    <apex:column value="{!p.Name}"/>
                    <apex:column value="{!p.Gender__c}"/>
                    <apex:column value="{!p.Email__c}"/>
                    <apex:column value="{!p.Doctor__c }"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
public with sharing class DynamicSearchPatientClass {
    Private string soql {get;set;}
    public Patient__c objPatients {get;set;}
    public List<Patient__c> patients {get;set;}
    public String sortDir{
        get { if (sortDir == null) { sortDir = 'asc'; }
        return sortDir;}
        set;
    }    
    public String sortField {
        get {if  ( sortField == null) {
        sortField = 'Name'; } return sortField; }
        set;
    }    
    public void runQuery() {
        try {
            patients= Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20 ');
            system.debug('>>>>>>4' +patients);
        }
        catch (Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, ' OOOps! ') );
        }
    }    
    public DynamicSearchPatientClass(){
        soql='select name,Email__c,Gender__c,Doctor__c from patient__c where patient__c.Name != null';
        system.debug('>>>>>>' +soql);
        runQuery();    
    }    
    public PageReference runSearch(){
        String Name = Apexpages.currentPage().getParameters().get('Name');
        String Gender = Apexpages.currentPage().getParameters().get('Gender');
        String Email = Apexpages.currentPage().getParameters().get('Email');
        String Doctor= Apexpages.currentPage().getParameters().get('Doctor');        
        soql='select name,Email__c,Gender__c,Doctor__c from patient__c where Name != null';   
        system.debug('>>>>>>3' +soql);
        if (!Name.equals(''))
            soql += ' and Name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
            system.debug('>>>>>>1' +soql);
        if (!Email.equals(''))
            soql += ' and Email__c LIKE \''+String.escapeSingleQuotes(Email)+'%\'';
        if (!Gender.equals(''))
            system.debug('>>>>>>2' +soql);
            soql += ' and Gender__c LIKE \''+String.escapeSingleQuotes(Gender)+'%\'';
        if (!Doctor.equals(''))
            soql += ' and Doctor__c LIKE \''+String.escapeSingleQuotes(Doctor)+'%\'';        
        runQuery();        
        return null;    
    }
}


Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
SainSain
Hi Hitesh,

Thanks for your quick response,now it's working fine but i have one more issue with above code Gender(picklist) and doctor(parent) is lookup field, so please help me how to fetch the records based on this fields too.

Thanks in advance
Sain