Creating A Lightning Design Multi-Object AutoComplete Search Box In Salesforce Visualforce
Table of contents
Hello There! Welcome Back đ
Today we are gonna learn to create a lightning design multi-object AutoComplete search box for our records in Salesforce Visualforce.
Things we will be using:
Salesforce Lightning Design System
Salesforce AJAX Toolkit A.K.A sforce
jQuery
Step 1: Gathering the resources we need.
Salesforce Lightning Design System:
Download the Custom Scoped CSS form here with your desired Scoping Class. (be sure to prefix your scoping class with a dot)
The Scoping class wraps your content using the Lightning Design System to avoid CSS conflicts.
I am using âsldsâ as a scoping class in my case.The file you download will be named âsalesforce-lightning-design-system.zipâ, just rename it to something convenient and short like âLDSâ.
Upload the zip file as your Static Resource.
Salesforce AJAX Toolkit A.K.A sforce:
This is provided by Salesforce out of the box, just like including a file from a directory.
<script src="../../soap/ajax/36.0/connection.js" type="text/javascript"></script>
jQuery:
We will be using CDN for this here, you can always have it as a static resource. Either way is fine.
<script src="
https://code.jquery.com/jquery-1.11.1.min.js"></script>
Now we have all the stuff we need, lets move to the code part.
Step 2: The Code.
First we put all the resources into the page. (Lines 2 to 11)
Declaring the sessionId variable (Important & Needed to work with Salesforce AJAX Toolkit). (Lines 7 to 9)
Create SLDS Lookup Component as shown in the SLDS website here, but without the List items as we are gonna dynamically add the list items to it. (Lines 13 to 40)
To know more on getting started with The Lightning Design System you can refer my another blog post.Query all for the records as per the input and displaying it in the dropdown. (Lines 49 to 88)
Getting a particular recordâs details upon selecting it from the search results list. (Lines 90 to 98 & 100 to 108)
<apex:page>
<!â Lightning Design System Source â>
<apex:stylesheet value="{!URLFOR($Resource.SLDS102, âassets/styles/salesforce-lightning-design-system-vf.cssâ)}" />
<!â jQuery CDN â>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<!â Declare SessionId Variable For AJAX Toolkit â>
<script type="text/javascript">
var __sfdcSessionId = â{!GETSESSIONID()}â;
</script>
<!â Salesforce AJAX Toolkit Source â>
<script src="../../soap/ajax/36.0/connection.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<div class="slds">
<div class="slds-lookup" data-select="multi" data-scope="single" data-typeahead="true">
<div class="slds-form-element">
<label class="slds-form-element__label" for="lookup">Search</label>
<div class="slds-form-element__control slds-input-has-icon slds-input-has-iconâright">
<svg aria-hidden="true" class="slds-input__icon slds-icon-text-default">
<use xlink:href="{!$Resource.SLDS102}/assets/icons/utility-sprite/svg/symbols.svg#search"></use>
</svg>
<!â Input With onkeyup Attribute To Search For Records For Every Value Entered â>
<input id="searchInput" class="slds-input" type="text" placeholder="Search Records" onkeyup="searchActs(j$(this))" aria-autocomplete="list" role="combobox" aria-expanded="true" aria-activedescendant="SrchDrpDwn" />
</div>
</div>
<div class="slds-lookup__menu" role="listbox" id="SrchDrpDwn" style="display:none;">
<div class="slds-lookup__item">
<button class="slds-button">
<svg aria-hidden="true" class="slds-icon slds-icon-text-default slds-iconâsmall">
<use xlink:href="{!$Resource.SLDS102}/assets/icons/utility-sprite/svg/symbols.svg#search"></use>
</svg>
"<span id="searchText"></span>" in Accounts & Leads
</button>
</div>
<!âSearch Results ULâ>
<ul class="slds-lookup__list" role="presentation" id="searchResultsUL" style="max-height: 240px;"></ul>
</div>
</div>
<!âDiv to Show Selected Record Detailsâ>
<div id="Info" style="width:510px;margin-left:15px;margin-top:15px;"></div>
</div>
<script>
var j$ = jQuery.noConflict();
function searchActs(key){
j$(â#â+key.attr(âaria-activedescendantâ)).show();
//Grabbing The Input Field Value
searchKey = key.val();
if(searchKey == â)
j$(â#â+key.attr(âaria-activedescendantâ)).hide();
j$(â#searchTextâ).text(searchKey);
//Querying For Records In Accounts Name Matching The Input in The Text Field
result = sforce.connection.query("Select Id, Name from Account WHERE Name LIKE â"+searchKey+"%â LIMIT 5");
records = result.getArray("records");
var searchLis ="<li class=\"slds-list__item slds-has-dividerâbottom-space\" style=\"padding-left: 5px; font-weight: 600; margin-bottom:3px; padding-bottom:3px; font-size: 18px;\">Accounts</li>";
//Creating List Elements Based on Query Results
if(records.length > 0){
for(var i=0; i<records.length; i++){
//List Elements With Onclick and ID Attributes
searchLis += â<li onclick="accInfo(\â+records[i].Id+â\â);" class="slds-lookup__item"><a id="â+records[i].Id+â" href="#" role="option"><svg aria-hidden="true" class="slds-icon slds-icon-standard-account slds-iconâsmall">â
+'<use xlink:href="{!$Resource.SLDS102}/assets/icons/standard-sprite/svg/symbols.svg#account"></use></svg>â+records[i].Name+'</a></li>â;
}
}else{
searchLis += â<li class="slds-lookup__item">No Records Found</li>â;
}
//Querying For Records In Leads Name Matching The Input in The Text Field
result = sforce.connection.query("Select Id, Name from Lead WHERE Name LIKE â"+searchKey+"%â LIMIT 5");
records = result.getArray("records");
searchLis +="<li class=\"slds-list__item slds-has-dividerâbottom-space\" style=\"padding-left: 5px; font-weight: 600; padding-bottom:3px; margin-bottom:3px; margin-top:6px; font-size: 18px;\">Leads</li>";
//Creating List Elements Based on Query Results
if(records.length > 0){
for(var i=0; i<records.length; i++){
//List Elements With Onclick and ID Attributes
searchLis += â<li onclick="leadInfo(\â+records[i].Id+â\â);" class="slds-lookup__item"><a id="â+records[i].Id+â" href="#" role="option"><svg aria-hidden="true" class="slds-icon slds-icon-standard-lead slds-iconâsmall">â
+'<use xlink:href="{!$Resource.SLDS102}/assets/icons/standard-sprite/svg/symbols.svg#lead"></use></svg>â+records[i].Name+'</a></li>â;
}
}else{
searchLis += â<li class="slds-lookup__item">No Records Found</li>â;
}
//Appending All The Created Result List Elements To the UL Tag
j$(â#searchResultsULâ).html(searchLis);
}
//Function To Get Account Recordâs Details on Record Click
function accInfo(actId){
j$(â#SrchDrpDwnâ).fadeOut();
//ID Based Retrieval Instead Of Query to Get Record Details Of The Selected Record
result = sforce.connection.retrieve("Name,BillingCountry", "Account", [actId]);
j$(â#searchInputâ).val(result[0].Name);
//Displaying The Record Details in Record Details Div
j$(â#Infoâ).html(â<u>Account</u><br/><strong>Name:</strong> â+result[0].Name+'<br/><strong>Billing Country:</strong> â+result[0].BillingCountry);
}
//Function To Get Lead Recordâs Details on Record Click
function leadInfo(ldId){
j$(â#SrchDrpDwnâ).fadeOut();
//ID Based Retrieval Instead Of Query to Get Record Details Of The Selected Record
result = sforce.connection.retrieve("Name,Status", "Lead", [ldId]);
j$(â#searchInputâ).val(result[0].Name);
//Displaying The Record Details in Record Details Div
j$(â#Infoâ).html(â<u>Lead</u><br/><strong>Name:</strong> â+result[0].Name+'<br/><strong>Lead Status:</strong> â+result[0].Status);
}
</script>
</html>
</apex:page>
That is it! And your search box will look like this:
Congrats, we just created a Dynamic Lightning Design style Multi Object AutoComplete Search Box. Amazing, Right?
You can explore the Salesforce AJAX Toolkit here.
If you like the content, give it some sharing :)