This is regarding searching.Search in salesforce has its own drawbacks especially with keywords and sorting.
Controller:
public with sharing class SearchFirstController {
protected List<List<sObject>> searchResults = new List<List<sObject>>{};public List<Lead> resultListLeads;
public List<Contact> resultListContacts;
public List<Account> resultListAccounts;
public String searchFirstname{ get; set; }
public String searchLastname{ get; set; }
public String searchCompany{ get; set; }
public String searchPhone{ get; set; }
public String searchEmail{ get; set; }
public boolean noLeadResult{ get; set; }
public boolean noContactResult{ get; set; }
public boolean noAccountResult{ get; set; }
public boolean emptyLeadList{ get; set; }
public boolean emptyContactList{ get; set; }
public boolean emptyAccountList{ get; set; }
public boolean errorShow{ get; set; }
public String errorTitle{ get; set; }
public String errorMessage{ get; set; }
public String errorSeverity{ get; set; }
public Integer numLeads{ get; set; }
public Integer numContacts{ get; set; }
public Integer numAccounts{ get; set; }
public SearchFirstController ( ) {
emptyLeadList = true;
emptyContactList = true;
emptyAccountList = true;
numLeads = 0;
numContacts = 0;
numAccounts = 0;
noLeadResult = false;
noContactResult = false;
noAccountResult = false;
errorShow = false;
errorTitle = '';
errorMessage = '';
errorSeverity = '';
}
public List<Lead> getresultListLeads() {
return resultListLeads;
}
public List<Contact> getresultListContacts() {
return resultListContacts;
}
public List<Account> getresultListAccounts() {
return resultListAccounts;
}
public PageReference search() {
errorShow = false;
string tablesFields = 'lead(id, phone, company, email, firstname, Status, lastname), contact(id, phone, email, firstname, lastname, accountId), account(id, phone, type, name)';
string searchString = getSearchCriteria();
string checkString = searchString.replace('*','').replace('?','');
if (checkString.length() > 1) {
try {
searchString = 'find \'' + searchString + '\' in ALL FIELDS RETURNING ' + tablesFields;
searchResults = search.query(searchString);
resultListLeads = ((List<Lead>)searchResults[0]);
numLeads = resultListLeads.size();
emptyLeadList = resultListLeads.isEmpty() ? true : false;
noLeadResult = emptyLeadList;
resultListContacts = ((List<Contact>)searchResults[1]);
numContacts = resultListContacts.size();
emptyContactList = resultListContacts.isEmpty() ? true : false;
noContactResult = emptyContactList;
resultListAccounts = ((List<Account>)searchResults[2]);
numAccounts = resultListAccounts.size();
emptyAccountList = resultListAccounts.isEmpty() ? true : false;
noAccountResult = emptyAccountList;
} catch (Exception e) {
// System.debug('Error when executing the search: ' + e);
errorTitle = e.getMessage();
errorMessage = e.getMessage();
errorSeverity = 'error';
errorShow = true;
}
} else if (checkString.length() == 1) {
numLeads = 0;
numContacts = 0;
numAccounts = 0;
emptyLeadList = true;
emptyContactList = true;
emptyAccountList = true;
noLeadResult = false;
noContactResult = false;
noAccountResult = false;
errorTitle = System.Label.sfError;
errorMessage = System.Label.sfErrorMessage ;
errorSeverity = 'error';
errorShow = true;
}
// return to the same page
return null;
}
public PageReference reset() {
numLeads = 0;
numContacts = 0;
numAccounts = 0;
emptyLeadList = true;
emptyContactList = true;
emptyAccountList = true;
searchFirstname = '';
searchLastname = '';
searchCompany = '';
searchPhone = '';
searchEmail = '';
errorTitle = '';
errorMessage = '';
errorSeverity = '';
errorShow = false;
noLeadResult = false;
noContactResult = false;
noAccountResult = false;
return null;
}
private string getSearchCriteria() {
String fullSearchString = '';
String searchName = searchFirstname;
if (searchName != ''){
if (searchLastname != '') {
searchName = searchFirstname + ' ' + searchLastname;
}
} else {
searchName = searchLastname;
}
fullSearchString = appendToSearchString(fullSearchString, searchName);
fullSearchString = appendToSearchString(fullSearchString, searchCompany);
fullSearchString = appendToSearchString(fullSearchString, searchPhone);
fullSearchString = appendToSearchString(fullSearchString, searchEmail);
return fullSearchString;
}
public PageReference createNewLead(){
PageReference pageRef = new PageReference('/00Q/e?retURL=%2F00Q%2Fo' + (searchCompany != ''?'&lea3='+ searchCompany : '') + (searchFirstname != ''?'&name_firstlea2='+ searchFirstname : '') + (searchLastname != ''?'&name_lastlea2='+ searchLastname : '') + (searchPhone != ''?'&lea8='+ searchPhone : '') + (searchEmail != ''?'&lea11='+ searchEmail : ''));
return pageRef;
}
public PageReference createNewContact(){
PageReference pageRef = new PageReference('/003/e?retURL=%2F003%2Fo' + (searchFirstname != ''?'&name_firstcon2='+ searchFirstname : '') + (searchLastname != ''?'&name_lastcon2='+ searchLastname : '') + (searchPhone != ''?'&con10='+ searchPhone : '') + (searchEmail != ''?'&con15='+ searchEmail : ''));
return pageRef; // + (searchCompany != ''?'&con4='+ searchCompany : '')
}
public PageReference createNewAccount(){
PageReference pageRef = new PageReference('/001/e?retURL=%2F001%2Fo' + (searchCompany != ''?'&acc2='+ searchCompany : '') );
return pageRef;
}
private string appendToSearchString(String searchString, String newValue) {
if (newValue != '') {
if (searchString == '') {
searchString = newValue;
} else {
searchString += ' OR ' + newValue;
}
}
return searchString;
}
}
VISUALFORCE PAGE:
<apex:page controller="SearchFirstController" tabStyle="Search_Create__tab">
<apex:sectionHeader title="{!$Label.sfTitle}" subtitle="Search For Accounts and Contacts" description="{!$Label.sfTitleDescription}"/>
<apex:outputPanel id="errorPanel">
<apex:pageMessage strength="2" title="{!errorTitle}" summary="{!errorMessage}" severity="{!errorSeverity}" rendered="{!errorShow}" />
</apex:outputPanel>
<apex:outputPanel id="searchPanel" styleClass="searchFilterFields">
<apex:form id="searchForm">
<div class="searchFilterFieldsHolder">
<table class="searchFilterFields" width="100%">
<tr>
<td width="250px">
<table width="100%">
<tr>
<td>
<apex:outputLabel value="{!$Label.sfAccount}" for="searchCompany"/>
</td>
<td width="100%">
<apex:inputText value="{!searchCompany}" id="searchCompany" required="false"/>
</td>
</tr>
<tr>
<tr>
<td>
<apex:outputLabel value="{!$Label.sfFirstName}" for="searchFirstName"/>
</td>
<td width="100%">
<apex:inputText value="{!searchFirstName}" id="searchFirstName" required="false"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel value="{!$Label.sfLastName}" for="searchLastName"/>
</td>
<td width="100%">
<apex:inputText value="{!searchLastname}" id="searchLastName" required="false"/>
</td>
</tr>
<td>
<apex:outputLabel value="{!$Label.sfPhone}" for="searchdPhone"/>
</td>
<td width="100%">
<apex:inputText value="{!searchPhone}" id="searchPhone" required="false"/>
</td>
</tr>
<tr>
<td>
<apex:outputLabel value="{!$Label.sfEmail}" for="searchEmail"/>
</td>
<td width="100%">
<apex:inputText value="{!searchEmail}" id="searchEmail" required="false"/>
</td>
</tr>
<tr>
<td>
</td>
<td width="100%">
<apex:commandButton value="{!$Label.btnSearch}" action="{!search}" rerender="resultPanel,errorPanel" styleClass="searchFilterButton" status="processingStatus"/> <apex:commandButton value="{!$Label.btnReset}" action="{!reset}" rerender="searchPanel,resultPanel,errorPanel" styleClass="searchFilterButton"/>
</td>
</tr>
</table>
</td>
<td align="center" valign="middle"><table style="font-size:80%;"><tr><td width="20%" align="right"><strong>{!$Label.sfAsterik}</strong></td><td width="80%">{!$Label.sfAsterikText}</td></tr><tr><td width="20%" align="right"><strong>{!$Label.sfQuestionmark}</strong></td><td width="80%">{!$Label.sfQuestionmarkText}</td></tr></table></td></tr></table>
</div>
</apex:form>
<center><apex:actionStatus id="processingStatus" startText="{!$Label.sfProcessRequest}"/></center>
</apex:outputPanel>
<br/>
<apex:outputPanel id="resultPanel">
<apex:form id="resultForm">
<apex:pageBlock id="accountresultPanel" title="{!$Label.sfAccounts}{!If(numAccounts > 0,' [' & text(numAccounts) & ']',' [0]')}" tabStyle="Account">
<apex:pageMessage strength="2" severity="info" summary="{!$Label.sfNoAccountResultWarning}" rendered="{!noAccountResult}" />
<apex:pageBlockSection title="{!$Label.sfMatchingAccounts}" collapsible="true" columns="1" rendered="{!NOT(emptyAccountList)}">
<apex:pageBlockTable value="{!resultListAccounts}" var="accountItem">
<apex:column >
<apex:facet name="header">{!$Label.sfAccountName}</apex:facet>
<apex:outputLink value="{!URLFOR($Action.Account.View, accountItem.Id)}">{!accountItem.Name}</apex:outputLink>
</apex:column>
<apex:column value="{!accountItem.Phone}"/>
<apex:column value="{!accountItem.Type}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:commandButton id="createNewAccount" value="{!$Label.btnNew}" action="{!createNewAccount}"/>
</apex:pageBlock>
<apex:pageBlock id="contactresultPanel" title="{!$Label.sfContacts}{!If(numContacts > 0,' [' & text(numContacts) & ']',' [0]')}" tabStyle="Contact">
<apex:pageMessage strength="2" severity="info" summary="{!$Label.sfNoContactResultWarning}" rendered="{!noContactResult}" />
<apex:pageBlockSection title="{!$Label.sfMatchingContacts}" collapsible="true" columns="1" rendered="{!NOT(emptyContactList)}">
<apex:pageBlockTable value="{!resultListContacts}" var="contactItem">
<apex:column >
<apex:facet name="header">{!$Label.sfContactName}</apex:facet>
<apex:outputLink value="{!URLFOR($Action.Contact.View, contactItem.Id)}">{!contactItem.Firstname} {!contactItem.Lastname}</apex:outputLink>
</apex:column>
<!-- <apex:column value="{!contactItem.Company}"/>-->
<apex:column value="{!contactItem.Phone}"/>
<apex:column value="{!contactItem.Email}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:commandButton id="createNewLead" value="{!$Label.btnNew}" action="{!createNewContact}"/>
</apex:pageBlock>
</apex:form>
</apex:outputPanel>
</apex:page>
TEST CLASS:
@isTest private class SearchFirstTest { static testMethod void myUnitTest() { Test.starttest(); PageReference testPage = Page.searchfirst; Test.setCurrentPage(testPage); SearchFirstController myController = new SearchFirstController(); myController.searchFirstname = ''; myController.searchLastname = ''; myController.searchCompany = ''; myController.searchPhone = ''; myController.searchEmail = ''; myController.noLeadResult = true; myController.noContactResult = true; myController.noAccountResult = true; myController.emptyLeadList = true; myController.emptyContactList = true; myController.emptyAccountList = true; myController.numLeads = 0; myController.numContacts = 0; myController.numAccounts = 0; myController.errorShow = true; myController.errorTitle = ''; myController.errorMessage = ''; myController.errorSeverity = ''; myController.getresultListLeads(); myController.getresultListContacts(); myController.getresultListAccounts(); System.assertEquals('',myController.searchFirstname); System.assertEquals('',myController.searchLastname); System.assertEquals('',myController.searchCompany); System.assertEquals('',myController.searchPhone); System.assertEquals('',myController.searchEmail); System.assert(myController.noLeadResult); System.assert(myController.noContactResult); System.assert(myController.noAccountResult); System.assert(myController.emptyLeadList); System.assert(myController.emptyContactList); System.assert(myController.emptyAccountList); System.assert(myController.errorShow); System.assertEquals('',myController.errorTitle); System.assertEquals('',myController.errorMessage); System.assertEquals('',myController.errorSeverity); myController.search(); myController.createNewLead(); myController.createNewContact(); myController.createNewAccount(); myController.searchFirstname = 'Test'; myController.searchLastname = ''; myController.searchCompany = 'Test'; myController.searchPhone = '345345'; myController.searchEmail = '435345@test.com'; myController.search(); myController.searchFirstname = ''; myController.searchLastname = 'Test'; myController.searchCompany = 'Test'; myController.searchPhone = '345345'; myController.searchEmail = '435345@test.com'; myController.search(); myController.searchFirstname = 'Test'; myController.searchLastname = 'Test'; myController.searchCompany = 'Test'; myController.searchPhone = '345345'; myController.searchEmail = '435345@test.com'; myController.search(); myController.searchFirstname = ''; myController.searchLastname = ''; myController.searchCompany = 'x'; myController.searchPhone = ''; myController.searchEmail = ''; myController.search(); myController.createNewLead(); myController.createNewContact(); myController.createNewAccount(); myController.reset(); Test.stoptest(); } } |

No comments:
Post a Comment