Concepts or coding lessons of Salesforce that you can implement easily

What Is return type of a SOSL search ,Really?

how to do it in salesforcerce
Salesforce Object Search Language (SOSL) has the ability to search across multiple objects in one query.

SOSL statements return a list of lists of sObjects, where each list contains the search results for a particular sObject type. The SOSL statements result lists are always returned in the same order as they were specified in the SOSL query. 
If a SOSL statements query does not return any records for a specified sObject type, the search results contain an empty list for that sObject.

Simply say return type of SOSL is List of List of sObjects

For Example :

List<List<sobject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Case, Opportunity, Lead];

//From above searchList, I am creating arrays for each object which are returned:

Account [] accounts = ((List<account>)searchList[0]);
Case []Case = ((List< Case >)searchList[1]);
Opportunity [] opportunities = ((List<opportunity>)searchList[2]);
Lead [] leads = ((List<lead>)searchList[3]);


The syntax of the FIND clause in the SOAP API and REST API differs from the syntax of the FIND clause in Apex differs from.

In Apex, the value of the FIND clause is separated with single quotes. 

For example:

FIND 'map*' IN ALL FIELDS RETURNING Account (Id, Name), Opportunity, Lead, Contact, User

In the Force.com API, the value of the FIND clause is separated with braces. 

For example:

FIND {map*} IN ALL FIELDS RETURNING Account (Id, Name), Opportunity, Lead, Contact, User



When to Use SOSL

Use SOSL in below scenarios when you don’t know which object or field the data resides in, and you want to:
  • You want to retrieve data for a specific name that you know exists within a field. Because SOSL can add multiple terms into a field and build a search index from this. So because of this, SOSL searches are faster and can return more relevant results.
  • Wheather the objects might or might not be related to one another, it retrieves multiple objects and fields efficiently.
  • With the help of division's feature, you can retrieve specific data for a particular division in an organization.
  • If you want to retrieve language specific data that’s in Chinese, Japanese, Korean, or Thai. For this use, Morphological tokenization for CJKT terms. This helps to ensure accurate results.
SOSL Examples:


1. Look for the name Nitish Talekar in the name field of an Account, return the ID field of the records.

FIND {Nitish Talekar}
IN Name Fields
RETURNING Account

2. Look for the name Nitish Talekar in the name field of an Account and return the name and phone number.

FIND {Nitish Talekar}
IN Name Fields
RETURNING Account(name, phone)

3. Look for the name Nitish Talekar or Sri Nivas in the name field of an Account or contact and return the name and phone number. If an opportunity is called Nitish Talekar or Sri Nivas, the opportunity should not be returned.

FIND {"Nitish Talekar" OR "Sri Nivas"}
IN Name Fields
RETURNING Account(name, phone), contact(name, phone)

4. Wildcards:


FIND {Nitish Ta*}

FIND {Nitish Ta?le*}

If you want a full description of SOSL query syntax, then check out the Salesforce SOQL and SOSL Reference Guide.

Enjoy! If you have any questions, comments, please feel free to let me know. 
As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

loading...

1 comment:

  1. I am truly inspired by this online journal! Extremely clear clarification of issues is given and it is open to every living soul. I have perused your post, truly you have given this extraordinary informative data about it.persoonlijke ontwikkeling amsterdam

    ReplyDelete