soql

SOSL (Salesforce Object Search language)

Salesforce Object Search Language (SOQL) is a Salesforce search language that is used to perform text searches in records. Use SOSL to search fields across multiple standard and custom object records in Salesforce.

Using the Query Editor

The Developer Console provides the Query Editor console, which enables you to run SOSL queries and view results.

Basic SOSL Syntax

This is the syntax of a basic SOSL query:


FIND  ‘SearchQuery’ [IN SearchGroup] [RETURNING ObjectsAndFields];


SearchQuery  is the text to search for (a single word or a phrase). Search terms can be grouped with logical operators (AND, OR) and parentheses. Also, search terms can include wildcard characters (*,?). The * wildcard matches zero or more characters at the middle or end of the search term. The ? wildcard matches only one character at the middle or end of the search term.

SearchGroup is optional. It is the scope of the fields to search. If not specified, the default search scope is all fields.SearchGroup can take one of the following values.

ALL FIELDS

NAME FIELDS

EMAIL FIELDS

PHONE FIELDS

SIDEBAR FIELDS

ObjectsAndFields  is optional. It is the information to return in the search result – a list of one or more sObjects and, within each sObject, list of one or more fields, with optional values to filter against. If nor specified, the search result contain the Ids of all objects found.

Single Words and Phrases

A Searchquery  contains two types of text:

Single Word – single word, such as test or hello. Words in the SearchQuery are delimited by spaces, punctuation, and changes from letters to digits (and vice-versa). Words are always case insensitive.

Phrase- collection of words and spaces surrounded by double quotes such as “john smith”. Multiple words can be combined together with logic and grouping operators to form a more complex query.

SOSL Apex Example

FIND{Wingo} IN ALL FIELDS RETURNING Account (Name),

Contact(FirstName,LastName,Deaprtment)

 

The SOSL query returns records that have fields whose values matches Wingo. Based on our sample data, only the contact has a field with the value Wingo, so this contact is returned.

The search query in the Query Editor and the API must be enclosed within curly brackets ({Wingo}). In contrast, in Apex the search query is enclosed within single quotes (‘Wingo’).

This is an example of a SOSL query that searches for accounts and contacts that have any fields with the word ‘SFDC’.

List<List<SObject>>searchList = [FIND ‘SFDC’IN ALL FIELDS

RETURNING Account(Name),

Contact(FirstName,LastName)];

Differences and Similarities Between SOQL and SOSL

Like SOQL, SOSL allows you to search your organization’s records for specific information. Unlike SOQL, which can only query one standard or custom object at a time, a single SOSL query can search all objects.

 

Another difference is that SOSL matches fields based on a word match while SOQL performs an exact match by default (when not using wildcards). For example, searching for ‘Digital’ in SOSL returns records whose field values are ‘Digital’ or ‘The Digital Company’, but SOQL returns only records with field values or ‘Digital’.

Use SOQL to retrieve records for a single object.

Use SOSL to search fields across multiple objects. SOSL queries can search most text fields on an object.

Hits: 1187

Share Post

By Himanshu Rana

My Name is Himanshu Rana, 23 Years young, born and grow up in Ghaziabad, India. A High Spirited Salesforce Admin, Developer and a Blogger. I currently work at Wakencode Technologies,

Leave a Reply

Your email address will not be published. Required fields are marked *