Dynamic SOSL Query on the fly in Apex

sfdcpanda, salesforce panda, panda sosl, apex, vf, sosl example, dineshkumar rajamani, dinesh dk, salesforce developer, dineshkumar rajamani linkedin,sfdc panda, salesforce interview questions, salesforce chennai, salesforce training in chennai, salesforce training chennai
SFDC PANDA Study mode on for SOSL

Dynamic SOSL Query Technical Explanation :-

Salesforce Object search language widely called as SOSL, SOSL queries assess to a list of list of sObjects(salesforce objects) where each list contains the results of particular sObject given in SOSL query.

It will return record list as specified in SOSL Query and if the specified sObject doesn’t return any records means search result will include an empty list for that sObject.

SOSL Query Example returning a list of list of sObjects: –

List<List<SObject>> searchList = [FIND ‘Dinesh*’ IN ALL FIELDS RETURNING Account (Id, Name), Contact (Id, Name), Opportunity (Id, Name)];

SOSL Query Execution, sfdc panda, salesfore panda, dynamic sosl, soql vs sosl, list of list of sObjects, dynamic query in salesforce, panda soql, sosl panda

Governor Limits enforced for SOSL :-

  • Total number of SOSL queries issued per Apex transaction is 20.
  • Total number of records retrieved by a single SOSL query per Apex transaction is 2,000.

Runtime Error :-

  • If you try to access a field that was not selected in the SOSL query (other than ID), you receive a runtime error, even if the field contains a data in the database.

Dynamic SOSL query source code Example (Apex Class) :-

Public with sharing class SOSLController{
Public List<Opportunity> optyList {get;set;}
Public List<contact> conList{get;set;}
Public List<account> accList{get;set;}

Public String searchStr{get;set;}
Public SOSLController(){
}

Public void soslDemo_method(){
optyList = New List<Opportunity>();
conList = New List<contact>();
accList = New List<account>();
if(searchStr.length() > 1){
String searchStr1 = ‘*’+searchStr+’*’;
String searchQuery = ‘FIND \” + searchStr1 + ‘\’ IN ALL FIELDS RETURNING  Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName)’;
List<List <sObject>> searchList = search.query(searchQuery);
accList = ((List<Account>)searchList[0]);
conList  = ((List<contact>)searchList[1]);
optyList = ((List<Opportunity>)searchList[2]);
System.debug(‘dk..’+searchList +’:account:’+accList );
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 two characters..’));
return;
}
}
}

VF Page :-

In VF page, Whenever i click on command button I am displaying a spinner until server request is done using actionstatus VF tag,

When the server returns a response, i am re-rendering( refreshing) all page block id’s to load with lastest values returned from server using ‘rerender’ VF tag..

<apex:page controller=”SOSLController”>
<apex:form >
<apex:inputText value=”{!searchStr}”/>
<apex:commandButton value=”SOSL search in Account, Contact, Opportunity” action=”{!soslDemo_method}” 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>

For more on Pagination :- http://www.sfdcpanda.com/salesforce-pagination-explained/

For more on SOQL :- http://www.sfdcpanda.com/soql-query-counts-number-active-contacts-account-set/

For more from salesforce developer guide :- Apex dynamic SOSL

Be the first to comment

Leave a Reply

Your email address will not be published.


*