+ Start a Discussion
SainSain 

Fetching data based on given search in Dynamic Search page

Hi,

I have custom object called patient and fields name, email__c, gender__c(piclist), Doctor__c(lookup field).
I developed 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. 
I am able to get data based on name, email__c, but iam facing problem with fetching data based on search given in picklist field(Gender__c), Doctor__c(lookup field)..

please help me modify code to solve this issue.

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;   
 }
}

Regards,
Sain