Experience Salesforce
Search Groups in SOSL | Return Specific Fields
What You’ll Learn
- What Is Search Groups in SOSL
- Returning
- Return Specified 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 { }.
Next Topic
Need Extra Support? Our FREE study materials have got you covered.
Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.