Hi All,
The ability of Apex and Visualforce was well depicted in this Dynamic Search Controller for Contacts post.
|
Dynamic Search Controller for Contacts |
It makes use of FirstName, LastName, LeadSource fields from Contact Object for the purpose of seting filter criteria. Based on these filter criteria, the Search Results Page Block table displays the results. Note that, we don't need to click "Refresh" or "Search" button as it performs the action dynamically based on the changes in the filters.
I have rendered three radio buttons, for the purpose of classifying the displayed results based on with or without account criteria as shown below.
VisualForce Page :
<apex:page controller="CustomContactSearchCont" sidebar="false">
<apex:outputLink value="http://www.salesforceforall.blogspot.com">
<apex:sectionHeader title="Contact" subtitle="Search" help="http://www.salesforceforall.blogspot.com"/>
</apex:outputLink>
<apex:pageMessages />
<script type="text/javascript">
var buttonValue="withwithout";
function doSearch()
{
searchServer(document.getElementById("firstName").value,
document.getElementById("lastName").value,
document.getElementById("leadSource").options[document.getElementById("leadSource").selectedIndex].value, buttonValue);
}
function whichButton()
{
for (i = 0; i < document.myForm.group.length; i++)
{
if (document.myForm.group[i].checked)
buttonValue = document.myForm.group[i].value;
}
doSearch();
}
function doo(fname,lname,aa)
{
if(window.confirm("You are about to delete the Contact "+fname+" "+lname+'\n'+"Are you sure?"))
{
delAcc(aa);
}
}
</script>
<apex:form >
<apex:actionFunction name="searchServer" action="{!runSearch}" reRender="results">
<apex:param name="firstName" value=""/>
<apex:param name="lastName" value=""/>
<apex:param name="leadSource" value=""/>
<apex:param name="butVal" value="{!selectedacc}"/>
</apex:actionFunction>
<apex:actionFunction name="delAcc" action="{!delTheAccount}" reRender="results">
<apex:param name="identity" value="" assignTo="{!selectedacc}"/>
</apex:actionFunction>
</apex:form>
<form name="myForm">
<table><tr><td valign="top" width="20%">
<apex:pageBlock title="Search Criteria" mode="edit">
<table>
<tr><th>{!FirstNameLabel}</th></tr>
<tr><td><input type="text" id="firstName" onkeyup="doSearch();"/></td></tr>
<tr><th>{!LastNameLabel}</th></tr>
<tr><td><input type="text" id="lastName" onkeyup="doSearch();"/></td></tr>
<tr><th>{!LeadSourceLabel}</th></tr>
<tr><td><select onchange="doSearch()" id="leadSource">
<option></option>
<apex:repeat value="{!LeadSourceVal}" var="values">
<option>{!values}</option>
</apex:repeat>
</select></td></tr>
<tr><td><br/><br/></td></tr>
<tr><th>with Account</th><th><input type="radio" name="group" value="with" id="with" onclick="whichButton()" /></th></tr>
<tr><th>without Account</th><th><input type="radio" name="group" value="without" id="without" onclick="whichButton()"/></th></tr>
<tr><th>with/without Account</th><th><input type="radio" name="group" value="withwithout" id="withwithout" onclick="whichButton()" checked="true" /></th></tr>
</table>
</apex:pageBlock>
</td>
<td width="80%" valign="top">
<apex:pageBlock title="Search Results" mode="detail">
<apex:outputPanel id="results">
<apex:pageBlockTable value="{!cons}" var="con">
<apex:column headerValue="Action">
<apex:outputLink title="" value="/{!con.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink> |
<a href="#" onclick="doo('{!con.FirstName}','{!con.LastName}','{!con.Id}');"><b>Del</b></a>
</apex:column>
<apex:column headerValue="{!FullNameLabel}">
<apex:outputLink value="/{!con.Id}">
{!con.Name}
</apex:outputLink>
</apex:column>
<apex:column value="{!con.FirstName}" headerValue="{!FirstNameLabel}"/>
<apex:column value="{!con.LastName}" headerValue="{!LastNameLabel}"/>
<apex:column value="{!con.account.Name}" headerValue="Account Name"/>
<apex:column value="{!con.LeadSource}" headerValue="{!LeadSourceLabel}"/>
</apex:pageBlockTable>
</apex:outputPanel>
</apex:pageBlock>
</td></tr></table></form>
<center><apex:outputLink value="http://www.salesforceforall.blogspot.com"><b>Love Your Technology</b></apex:outputLink></center>
</apex:page>
Controller :
public with sharing class CustomContactSearchCont
{
public List<Contact> cons{get;set;}
public String strQuery;
public String bv='';
public String selectedacc{get;set;}
public CustomContactSearchCont()
{
initstrQuery();
cons=Database.Query(strQuery);
}
public void initstrQuery()
{
if(bv==''||bv=='withwithout')
strQuery='SELECT Name, FirstName, LastName, Account.Name, LeadSource from Contact WHERE Id!=null';
if(bv=='with')
strQuery='SELECT Name, FirstName, LastName, Account.Name, LeadSource from Contact WHERE Account.Name!=null';
if(bv=='without')
strQuery='SELECT Name, FirstName, LastName, Account.Name, LeadSource from Contact WHERE Account.Name=null';
}
public PageReference runSearch()
{
String fn=ApexPages.currentPage().getParameters().get('firstName');
String ln=ApexPages.currentPage().getParameters().get('lastName');
String ls=ApexPages.currentPage().getParameters().get('leadSource');
bv=ApexPages.currentPage().getParameters().get('butVal');
initstrQuery();
if(!fn.equals(''))
strQuery+=' AND FirstName LIKE \''+fn+'%\'';
if(!ln.equals(''))
strQuery+=' AND LastName LIKE \''+ln+'%\'';
if(!ls.equals(''))
strQuery+=' AND LeadSource=:ls';
try
{
cons = Database.query(strQuery);
}
catch (Exception e)
{
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
}
return null;
}
public PageReference delTheAccount()
{
if(selectedacc!=null)
{
Contact ct= Database.Query('SELECT Id, Name, FirstName from Contact WHERE Id=:selectedacc');
Delete ct;
PageReference testPage = Page.CustomContactSearchPage;
testPage.setRedirect(true);
return testPage;
}
return null;
}
/*Getting Field Labels and Picklist Values*/
public string FullNameLabel
{
get
{
Schema.DescribeFieldResult f=Contact.Name.getDescribe();
return (string) f.getLabel();
}
set;
}
public string FirstNameLabel
{
get
{
Schema.DescribeFieldResult f=Contact.FirstName.getDescribe();
return (string) f.getLabel();
}
set;
}
public string LastNameLabel
{
get
{
Schema.DescribeFieldResult f=Contact.LastName.getDescribe();
return (string) f.getLabel();
}
}
public string LeadSourceLabel
{
get
{
Schema.DescribeFieldResult f=Contact.LeadSource.getDescribe();
return (string) f.getLabel();
}
set;
}
public List<String> LeadSourceVal
{
get
{
if (LeadSourceVal == null)
{
LeadSourceVal= new List<String>();
Schema.DescribeFieldResult field = Contact.LeadSource.getDescribe();
for (Schema.PicklistEntry f : field.getPicklistValues())
LeadSourceVal.add(f.getLabel());
}
return LeadSourceVal;
}
set;
}
}
Hope that, this post will be very useful for beginners.