Hello Guys,
I made advance search code for Contact object.
Hope this will help you guys to Make search easy in Salesforce object.
Visual Force Page
Controller for Above Page :
public with sharing class AdvanceSearch {
// the soql without the order and limit
private String soql {get;set;}
// the collection of contacts to display
public List<Contact> contacts {get;set;}
// the current sort direction. defaults to asc
public String sortDir {
get { if (sortDir == null) { sortDir = 'asc'; } return sortDir; }
set;
}
// the current field to sort by. defaults to last name
public String sortField {
get { if (sortField == null) {sortField = 'lastName'; } return sortField; }
set;
}
// format the soql for display on the visualforce page
public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
set;
}
// init the controller and display some sample data when the page loads
public AdvanceSearch() {
soql = 'select firstname, lastname, account.name, interested_technologies__c from contact where account.name != null';
runQuery();
}
// toggles the sorting of query from asc<-->desc
public void toggleSort() {
// simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
// run the query again
runQuery();
}
// runs the actual query
public void runQuery() {
try {
contacts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
}
}
// runs the search with parameters passed via Javascript
public PageReference runSearch() {
String firstName = Apexpages.currentPage().getParameters().get('firstname');
String lastName = Apexpages.currentPage().getParameters().get('lastname');
String accountName = Apexpages.currentPage().getParameters().get('accountName');
String technology = Apexpages.currentPage().getParameters().get('technology');
soql = 'select firstname, lastname, account.name, interested_technologies__c from contact where account.name != null';
if (!firstName.equals(''))
soql += ' and firstname LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
if (!lastName.equals(''))
soql += ' and lastname LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';
if (!accountName.equals(''))
soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\'';
if (!technology.equals(''))
soql += ' and interested_technologies__c = \''+technology+'\'';
// run the query again
runQuery();
return null;
}
// use apex describe to build the picklist values
public List<String> technologies {
get {
if (technologies == null) {
technologies = new List<String>();
Schema.DescribeFieldResult field = Contact.interested_technologies__c.getDescribe();
for (Schema.PicklistEntry f : field.getPicklistValues())
technologies.add(f.getLabel());
}
return technologies;
}
set;
}
}
I made advance search code for Contact object.
Hope this will help you guys to Make search easy in Salesforce object.
Visual Force Page
<apex:page controller="AdvanceSearch" sidebar="false">
<apex:form>
<apex:pagemessages id="errors">
<apex:pageblock mode="edit" title="Find Me A Customer!">
<table border="0" style="width: 100%px;">
<tbody>
<tr>
<td valign="top" width="200"><apex:pageblock id="criteria" mode="edit" title="Parameters">
<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 action="{!runSearch}" name="searchServer" 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:param></apex:param></apex:param></apex:param></apex:actionfunction>
<table cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="font-weight: bold;">First Name<br />
<input id="firstName" onkeyup="doSearch();" type="text" />
</td>
</tr>
<tr>
<td style="font-weight: bold;">Last Name<br />
<input id="lastName" onkeyup="doSearch();" type="text" />
</td>
</tr>
<tr>
<td style="font-weight: bold;">Account<br />
<input id="accountName" onkeyup="doSearch();" type="text" />
</td>
</tr>
<tr>
<td style="font-weight: bold;">Interested Technologies<br />
<select id="technology" onchange="doSearch();">
<option value=""></option>
<option value="{!tech}">{!tech}</option>
</select>
</td>
</tr>
</tbody></table>
</apex:pageblock>
</td>
<td valign="top"><apex:pageblock id="results" mode="edit">
<apex:pageblocktable value="{!contacts}" var="contact">
<apex:column>
<apex:facet name="header">
<apex:commandlink action="{!toggleSort}" rerender="results,debug" value="First Name">
<apex:param assignto="{!sortField}" name="sortField" value="firstName">
</apex:param></apex:commandlink>
</apex:facet>
<apex:outputfield value="{!contact.firstName}">
</apex:outputfield></apex:column>
<apex:column>
<apex:facet name="header">
<apex:commandlink action="{!toggleSort}" rerender="results,debug" value="Last Name">
<apex:param assignto="{!sortField}" name="sortField" value="lastName">
</apex:param></apex:commandlink>
</apex:facet>
<apex:outputfield value="{!contact.lastName}">
</apex:outputfield></apex:column>
<apex:column>
<apex:facet name="header">
<apex:commandlink action="{!toggleSort}" rerender="results,debug" value="Account">
<apex:param assignto="{!sortField}" name="sortField" value="account.name">
</apex:param></apex:commandlink>
</apex:facet>
<apex:outputfield value="{!contact.account.name}">
</apex:outputfield></apex:column>
<apex:column>
<apex:facet name="header">
<apex:commandlink action="{!toggleSort}" rerender="results,debug" value="Technologies">
<apex:param assignto="{!sortField}" name="sortField" value="interested_technologies__c">
</apex:param></apex:commandlink>
</apex:facet>
<apex:outputfield value="{!contact.interested_technologies__c}">
</apex:outputfield></apex:column>
</apex:pageblocktable>
</apex:pageblock>
</td>
</tr>
</tbody></table>
<apex:pageblock id="debug" title="Debug - SOQL">
<apex:outputtext value="{!debugSoql}">
</apex:outputtext></apex:pageblock>
</apex:pageblock>
</apex:pagemessages></apex:form>
</apex:page>
<apex:form>
<apex:pagemessages id="errors">
<apex:pageblock mode="edit" title="Find Me A Customer!">
<table border="0" style="width: 100%px;">
<tbody>
<tr>
<td valign="top" width="200"><apex:pageblock id="criteria" mode="edit" title="Parameters">
<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 action="{!runSearch}" name="searchServer" 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:param></apex:param></apex:param></apex:param></apex:actionfunction>
<table cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="font-weight: bold;">First Name<br />
<input id="firstName" onkeyup="doSearch();" type="text" />
</td>
</tr>
<tr>
<td style="font-weight: bold;">Last Name<br />
<input id="lastName" onkeyup="doSearch();" type="text" />
</td>
</tr>
<tr>
<td style="font-weight: bold;">Account<br />
<input id="accountName" onkeyup="doSearch();" type="text" />
</td>
</tr>
<tr>
<td style="font-weight: bold;">Interested Technologies<br />
<select id="technology" onchange="doSearch();">
<option value=""></option>
<option value="{!tech}">{!tech}</option>
</select>
</td>
</tr>
</tbody></table>
</apex:pageblock>
</td>
<td valign="top"><apex:pageblock id="results" mode="edit">
<apex:pageblocktable value="{!contacts}" var="contact">
<apex:column>
<apex:facet name="header">
<apex:commandlink action="{!toggleSort}" rerender="results,debug" value="First Name">
<apex:param assignto="{!sortField}" name="sortField" value="firstName">
</apex:param></apex:commandlink>
</apex:facet>
<apex:outputfield value="{!contact.firstName}">
</apex:outputfield></apex:column>
<apex:column>
<apex:facet name="header">
<apex:commandlink action="{!toggleSort}" rerender="results,debug" value="Last Name">
<apex:param assignto="{!sortField}" name="sortField" value="lastName">
</apex:param></apex:commandlink>
</apex:facet>
<apex:outputfield value="{!contact.lastName}">
</apex:outputfield></apex:column>
<apex:column>
<apex:facet name="header">
<apex:commandlink action="{!toggleSort}" rerender="results,debug" value="Account">
<apex:param assignto="{!sortField}" name="sortField" value="account.name">
</apex:param></apex:commandlink>
</apex:facet>
<apex:outputfield value="{!contact.account.name}">
</apex:outputfield></apex:column>
<apex:column>
<apex:facet name="header">
<apex:commandlink action="{!toggleSort}" rerender="results,debug" value="Technologies">
<apex:param assignto="{!sortField}" name="sortField" value="interested_technologies__c">
</apex:param></apex:commandlink>
</apex:facet>
<apex:outputfield value="{!contact.interested_technologies__c}">
</apex:outputfield></apex:column>
</apex:pageblocktable>
</apex:pageblock>
</td>
</tr>
</tbody></table>
<apex:pageblock id="debug" title="Debug - SOQL">
<apex:outputtext value="{!debugSoql}">
</apex:outputtext></apex:pageblock>
</apex:pageblock>
</apex:pagemessages></apex:form>
</apex:page>
Controller for Above Page :
public with sharing class AdvanceSearch {
// the soql without the order and limit
private String soql {get;set;}
// the collection of contacts to display
public List<Contact> contacts {get;set;}
// the current sort direction. defaults to asc
public String sortDir {
get { if (sortDir == null) { sortDir = 'asc'; } return sortDir; }
set;
}
// the current field to sort by. defaults to last name
public String sortField {
get { if (sortField == null) {sortField = 'lastName'; } return sortField; }
set;
}
// format the soql for display on the visualforce page
public String debugSoql {
get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }
set;
}
// init the controller and display some sample data when the page loads
public AdvanceSearch() {
soql = 'select firstname, lastname, account.name, interested_technologies__c from contact where account.name != null';
runQuery();
}
// toggles the sorting of query from asc<-->desc
public void toggleSort() {
// simply toggle the direction
sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
// run the query again
runQuery();
}
// runs the actual query
public void runQuery() {
try {
contacts = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
}
}
// runs the search with parameters passed via Javascript
public PageReference runSearch() {
String firstName = Apexpages.currentPage().getParameters().get('firstname');
String lastName = Apexpages.currentPage().getParameters().get('lastname');
String accountName = Apexpages.currentPage().getParameters().get('accountName');
String technology = Apexpages.currentPage().getParameters().get('technology');
soql = 'select firstname, lastname, account.name, interested_technologies__c from contact where account.name != null';
if (!firstName.equals(''))
soql += ' and firstname LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
if (!lastName.equals(''))
soql += ' and lastname LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';
if (!accountName.equals(''))
soql += ' and account.name LIKE \''+String.escapeSingleQuotes(accountName)+'%\'';
if (!technology.equals(''))
soql += ' and interested_technologies__c = \''+technology+'\'';
// run the query again
runQuery();
return null;
}
// use apex describe to build the picklist values
public List<String> technologies {
get {
if (technologies == null) {
technologies = new List<String>();
Schema.DescribeFieldResult field = Contact.interested_technologies__c.getDescribe();
for (Schema.PicklistEntry f : field.getPicklistValues())
technologies.add(f.getLabel());
}
return technologies;
}
set;
}
}
No comments:
Post a Comment