Learn from industry experts! Admin batch starts from 19 Feb.
Hurry up!
Book your slot now!

7

Search Groups in SOSL | Return Specific Fields

What Is Search Groups in SOSL

Search Groups are basically used to define the type of fields in which you can search that text.

Search in Specific Fields:

  • All Fields (By Default)
  • Name Fields
  • Email Fields
  • Phone Fields
  • Sidebar Fields

Returning

If we want to return texts from a particular object then we use returning keyword.

Ex:

List<List<sObject>> results = [FIND 'Univ*' IN NAME FIELDS RETURNING Account, Contact];
List<Account> accounts = (List<Account>)results[0];
List<Contact> contacts = (List<Contact>)results[1];

Return Specified Fields

 If we want to search text in a particular field, we are going to use search group.

Find {contact} IN (searchgroup)

  • All Fields (By Default)
  • Name Fields
  • Email Fields
  • Phone Fields

Syntex :

Find {contact} IN (searchgroup) returning objects & fields.

Example :

List<List<sObject>> results = [FIND 'Univ*' IN NAME FIELDS RETURNING
Account(Name, BillingCountry), Contact(FirstName, LastName)];
List<Account> accounts = (List<Account>)results[0];
system.debug(accounts[0].Name);
Note:
  • Return Type of SOSL is list of list of sObjects which is parent class of all Objects.
  •  In Apex use ‘ ’ instead of { }.

 

LWC Training
Next Topic

Need more support?

Get a head start with our FREE study notes!

Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.

Book A 15-Minutes Free Career Counselling Today!