Monday 20 January 2014

sosl salesforce example

sosl salesforce example

SOSL is a select query that returns list of lists. That is, it can return list of multiple objects. It searches a parameter within fields fo multiple objects as specified in the query.
In the below example string 'test' is being searched in 4 objects Opportunity,account,Lead and contact. You need to specify in which fields it should search this string.
For example, if you want it to search in name and description fields of account then it would look like  account( name,description)
The list of lists has list of objects in the order mentioned in the query. For example if you specify account as first object in query; then the account list will be available at 0th location of list of lists.
visualforce page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<apex:page controller="DeferenceDemoController">
  <apex:form >
    <apex:commandButton value="Show records using SOSL" action="{!soslDemo_method}"/>
    <apex:pageBlock title="Accounts">
       <apex:pageblockTable value="{!accList }" var="acc">
          <apex:column value="{!acc.name}"/>
          <apex:column value="{!acc.Type}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
 <apex:pageBlock title="Contacts">
    <apex:pageblockTable value="{!conList}" var="con">
      <apex:column value="{!con.name}"/>
      <apex:column value="{!con.email}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
 <apex:pageBlock title="Leads">
    <apex:pageblockTable value="{!leaList}" var="lea">
      <apex:column value="{!lea.name}"/>
      <apex:column value="{!lea.company}"/>
    </apex:pageblockTable>
 </apex:pageBlock>
 <apex:pageBlock title="Opportunities">
    <apex:pageblockTable value="{!optyList}" var="opty">
      <apex:column value="{!opty.name}"/>
     <apex:column value="{!opty.StageName}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
  </apex:form>
</apex:page>

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Public with sharing class DeferenceDemoController {
 Public List<Opportunity> optyList {get;set;}
 Public List<Lead> leaList{get;set;}
 Public List<contact> conList{get;set;}
 Public List<account> accList{get;set;}
   Public DeferenceDemoController(){
   }
  Public void soslDemo_method(){
   optyList = New List<Opportunity>();
   leaList = New List<Lead>();
   conList = New List<contact>();
   accList = New List<account>();
   List<List <sObject>> searchList = [FIND 'test' IN ALL FIELDS RETURNING  Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName),Lead(company,name,status) ];
   accList = ((List<Account>)searchList[0]);
   conList  = ((List<contact>)searchList[1]);
   optyList = ((List<Opportunity>)searchList[2]);
   leaList  = ((List<Lead>)searchList[3]);
  }
}





As of now the SOSL can return only 2000 records (Combined for all the objects that are queried in the SOSL query) thsi is as per governer's limit

No comments:

Post a Comment