20
NovDiwali Deal : Flat 20% off + 2 free self-paced courses + Free Ebook + $200 Voucher - SCHEDULE CALL
Use the Salesforce Object Search Language (SOSL) to construct text-based search queries against the search index. SOSL or Salesforce object query language searches for a particular string from multiple objects. It always returns a list object. The result lists are always returned in the same order as they were specified in the SOSL query. SOQL and SOQL in Salesforce are the most used language by developers.
SOSL (Salesforce Object Query Language) is similar to SOQL (Salesforce Object Query Language) but it can search for records from multiple Salesforce objects. We can search in numerous objects at the same time using SOSL. In SOQL, we can query only one object at a time but in SOSL, we can query multiple Salesforce objects together. To know more about Salesforce objects, you should study the Salesforce object model and how it works exactly. A salesforce object model helps you to map the data properly. Also, you can combine data logically as per the requirement.
Learn Salesforce in the Easiest Way
Read: A Basic Overview for Salesforce Security
If your searches are too general, they are slow and return too many results. Use the following clauses to define efficient text searches. You can also use SOQL (Salesforce Object Query language) if needed.
Salesforce Training For Administrators & Developers
List> accountSearchList =newList
>(); accountSearchList=[FIND {Joe Smith}IN Name Fields RETURNING lead (name, phone Where createddate = THIS_FISCAL_QUARTERLIMIT 20) ]; list
accs; accs=(list )result[0]; system.debug(accs); Example of SOSL Apex class Public with sharing class SOSLdemoExample{ Public List optyList {get;set;} Public List conList{get;set;} Public List accList{get;set;} Public String searchStr{get;set;} Public SOSLdemoExample(){ } Public void soslSearchmethod (){ optyList = New List (); conList = New List (); accList = New List (); if(searchStr.length() > 2){ String searchStr1 = '*'+searchStr+'*'; String searchQuery = 'FIND \'' + searchStr1 + '\' IN ALL FIELDS RETURNING Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName)'; List > searchList = search.query(searchQuery); accList = ((List
)searchList[0]); conList = ((List )searchList[1]); optyList = ((List )searchList[2]); if(accList.size() == 0 && conList.size() == 0 && optyList.size() == 0){ apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sory, no results returned with matching string..')); return; } } else{ apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least three characters..')); return; } } } Visualforce Page <apex:page controller="SOSLdemoExample"> <apex:form > <apex:inputText value="{!searchStr}"/> <apex:commandButton value="Search in Account, Contact, Opportunity" action="{!soslSearchmethod }" reRender="acct,error,oppt,cont" status="actStatusId"/> <apex:actionStatus id="actStatusId"> <apex:facet name="start" > <img src="/img/loading.gif"/> </apex:facet> </apex:actionStatus> </apex:form> <apex:outputPanel title="" id="error"> <apex:pageMessages ></apex:pageMessages> </apex:outputPanel> <apex:pageBlock title="Accounts" id="acct"> <apex:pageblockTable value="{!accList }" var="acc"> <apex:column value="{!acc.name}"/> <apex:column value="{!acc.Type}"/> </apex:pageblockTable> </apex:pageBlock> <apex:pageBlock title="Contacts" id="cont"> <apex:pageblockTable value="{!conList}" var="con"> <apex:column value="{!con.name}"/> <apex:column value="{!con.email}"/> </apex:pageblockTable> </apex:pageBlock> <apex:pageBlock title="Opportunities" id="oppt"> <apex:pageblockTable value="{!optyList}" var="opty"> <apex:column value="{!opty.name}"/> <apex:column value="{!opty.StageName}"/> </apex:pageblockTable> </apex:pageBlock> </apex:page>
Output
Read: Apex:Actionfunction In The Salesforce VisualForce Page
Salesforce Training For Administrators & Developers
SOQL and SOSL in Salesforce are two popular languages to manage Salesforce objects, Standard objects and custom objects. Also, it is easy to retrieve data with the help of these two-programming languages. It may be tough learning about the syntax of the language and the Salesforce object model but the constant effort and dedication can make things easier than expectations. All the Best!
Read: Salesforce Tutorial - Ultimate Learning Guide for Beginners & Beyond
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
Receive Latest Materials and Offers on Salesforce Course
Interviews