I tried SOSL for the first time in recently on 15th October, 2011 when I got an email from someone asking for help in developing a search functionality using visualforce.
His is requirements was simple, he wanted to search on contacts but wanted to use only single text box for searching on contacts. I had earlier developed a simple search functionality using SOQL and Java Script Remoting, so I just modified that page with SOSL working in the controller.
Here is the code for Visualforce page:
<apex:page controller="remoteTest"> <script> var contacts; function contactSearch(name) { myp1.remoteTest.findContacts(name,handleContacts); } function handleContacts(result, event) { if(event.type == 'exception') { alert(event.message); } else { contacts = result; showContacts(); } } function showContacts() { var newList = ""; for(var i = 0; i < contacts.length; i++) { newList += "<a href onclick='showContact("+i+")'>"+ contacts[i].Name+"</a><BR/>"; } document.getElementById('contactList').innerHTML = newList; } function showContact(index) { document.getElementById('phone').innerHTML = 'Phone: '+contacts[index].Phone; document.getElementById('email').innerHTML = 'Email: '+contacts[index].Email; } </script> <input id="nameField" type="text" onKeyDown="contactSearch(document.getElementById('nameField').value)" /> <button onChange="contactSearch(document.getElementById('nameField').value)">Search Contacts</button> <div style="width: 50%;"> <div id="contactList"></div> <div style="float: right;"> <div id="phone"></div> <div id="email"></div> </div> </div> </apex:page>Demo Page: http://testdomain5-developer-edition.na3.force.com/MySite
Here is the controller code:
global class remoteTest { @RemoteAction global static Contact[] findContacts(string Name) { Name = '%'+Name+'%'; Contact[] c = [SELECT ID, Name, Phone, Email from Contact where NAME LIKE :Name ]; return c; } }
Good topic to implement. it will help a lot. Please keep posted.
ReplyDeleteGreat stuff Sankalp..
ReplyDeleteawesome bro...keep up the good work..
ReplyDelete