-----------------------------------------------------------------------------------
Controller
------------------------------------------------------------------------------------
public class SearchEngine1 {
public String name { get; set; }
public String firstname {get;set;}
public String LastName{get;set;}
public String Title{get;set;}
public List<contact> searchResults {get;set;}
private boolean firstFilterApplied = false;
public SearchEngine1(){
searchResults = new List<contact>();
}
public PageReference search(){
searchResults.clear();
firstFilterApplied = false;
try{
//Create the dynamic SOQL query
String queryString = 'SELECT FirstName,LastName,Title '+ 'FROM contact';
if (LastName!= null && LastName!= ''){
LastName= String.escapeSingleQuotes(LastName); //To avoid SOQL injection
LastName= lastname.startsWith('%') ? LastName: '%' + LastName;
LastName= lastname.endsWith('%') ? lastname : lastname + '%';
queryString += whereOrAndClause() + ' LastName like \''+LastName+'\'';
}
system.debug('##########'+queryString );
searchResults = Database.query(queryString);
system.debug('##########'+searchResults);
}
catch(QueryException e){
//If the query returns more than 1000 records, display an error to the user
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'No records found with this criteria'));
return null;
}
catch(Exception e1){
ApexPages.addMessages(e1);
return null;
}
if (searchResults.size() == 0){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'No records found with this criteria'));
}
return null;
}
private String whereOrAndClause(){
String queryClause;
if (firstFilterApplied){
queryClause = ' AND ';
}
else{
queryClause = ' WHERE ';
}
firstFilterApplied = true;
return queryClause;
}
}
---------------------------------------------------------------------------------------------------------------
Dynamic Controller Page
-------------------------------------------------------------------------------------------------------------
<apex:page controller="SearchEngine1" tabStyle="Account" showHeader="true" sidebar="true" >
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Search Criteria">
<apex:pageBlockSection >
<apex:inputText value="{!LastName}" label="Last Name"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!search}" value="Search"/>
</apex:pageBlockButtons>
</apex:pageBlock>
<apex:pageBlock title="Search Result">
<apex:pageBlockTable value="{!searchResults}" var="res" rendered="{!searchResults.size > 0}">
<apex:column title="Final result">
<apex:outputLink value="/{!res.Id}" target="_blank">{!res.FirstName}</apex:outputLink>
</apex:column>
<apex:column title="Final result">
<apex:outputLink value="/{!res.Id}" target="_blank">{!res.LastName}</apex:outputLink>
</apex:column>
<!-- <apex:column title="Celebrity">
<apex:outputLink onmouseover="" value="/{!res.LastName__c}" target="_blank">{!res.celebrity__r.name}</apex:outputLink>
</apex:column> -->
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller
------------------------------------------------------------------------------------
public class SearchEngine1 {
public String name { get; set; }
public String firstname {get;set;}
public String LastName{get;set;}
public String Title{get;set;}
public List<contact> searchResults {get;set;}
private boolean firstFilterApplied = false;
public SearchEngine1(){
searchResults = new List<contact>();
}
public PageReference search(){
searchResults.clear();
firstFilterApplied = false;
try{
//Create the dynamic SOQL query
String queryString = 'SELECT FirstName,LastName,Title '+ 'FROM contact';
if (LastName!= null && LastName!= ''){
LastName= String.escapeSingleQuotes(LastName); //To avoid SOQL injection
LastName= lastname.startsWith('%') ? LastName: '%' + LastName;
LastName= lastname.endsWith('%') ? lastname : lastname + '%';
queryString += whereOrAndClause() + ' LastName like \''+LastName+'\'';
}
system.debug('##########'+queryString );
searchResults = Database.query(queryString);
system.debug('##########'+searchResults);
}
catch(QueryException e){
//If the query returns more than 1000 records, display an error to the user
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'No records found with this criteria'));
return null;
}
catch(Exception e1){
ApexPages.addMessages(e1);
return null;
}
if (searchResults.size() == 0){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'No records found with this criteria'));
}
return null;
}
private String whereOrAndClause(){
String queryClause;
if (firstFilterApplied){
queryClause = ' AND ';
}
else{
queryClause = ' WHERE ';
}
firstFilterApplied = true;
return queryClause;
}
}
---------------------------------------------------------------------------------------------------------------
Dynamic Controller Page
-------------------------------------------------------------------------------------------------------------
<apex:page controller="SearchEngine1" tabStyle="Account" showHeader="true" sidebar="true" >
<apex:form >
<apex:pageMessages />
<apex:pageBlock title="Search Criteria">
<apex:pageBlockSection >
<apex:inputText value="{!LastName}" label="Last Name"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!search}" value="Search"/>
</apex:pageBlockButtons>
</apex:pageBlock>
<apex:pageBlock title="Search Result">
<apex:pageBlockTable value="{!searchResults}" var="res" rendered="{!searchResults.size > 0}">
<apex:column title="Final result">
<apex:outputLink value="/{!res.Id}" target="_blank">{!res.FirstName}</apex:outputLink>
</apex:column>
<apex:column title="Final result">
<apex:outputLink value="/{!res.Id}" target="_blank">{!res.LastName}</apex:outputLink>
</apex:column>
<!-- <apex:column title="Celebrity">
<apex:outputLink onmouseover="" value="/{!res.LastName__c}" target="_blank">{!res.celebrity__r.name}</apex:outputLink>
</apex:column> -->
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
No comments:
Post a Comment