Showing posts with label SOSL Statements. Show all posts
Showing posts with label SOSL Statements. Show all posts

Friday, June 1, 2012

SOSL Statements


SOSL Statements
SOSL (Salesforce Object Search Language) statements estimate to the list of lists of salesforce Objects, where each list contains the search results for a particular salesforce Object type.
The result lists are always returned in the same order as they were specified in the SOSL (Salesforce Object Search Language) query.
  • SOSL queries are only supported to the Apex classes and the anonymous blocks.
  • You cannot use a SOSL query in the trigger.
  • If SOSL query does not return any records for a specified sObject type, and the search results include an empty list for that salesforce Object.
For example
You can return a list of accounts, contacts, opportunities, and leads that begin with phrase map utile:
SOSL:
List> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (id, name),Contact, Opportunity, Lead];
Description:
The syntax of the FIND clause in Apex differs from the syntax of the FIND clause in the Web services API:
• In Apex, the value of the FIND clause is demarcated with single quotes.
For example:
FIND 'map*' IN ALL FIELDS RETURNING Account (id, name), Contact, Opportunity, Lead.
• In the Force.com API, the value of the FIND clause is demarcated with braces.
Example of SOSL:
FIND {map*} IN ALL FIELDS RETURNING Account (id, name, address), Contact, Opportunity, Lead From search List, you can create arrays for each object returned as given below:
Account [] accounts = ((List) search List [0]); //Account object
Contact [] contacts = ((List) search List [1]); //Contact object
Opportunity [] opportunities = ((List) search List [2]); //Oppty
Lead [] leads = ((List) search List [3]); //Lead object
SOQL and SOSL statements in Apex can reference Apex code variables and the expressions if they are preceded by a colon (:).
This use of a local code variable within a SOQL or SOSL statement is called a bind (concatenate). The Apex parser first evaluates the local variable in code context before executing the SOQL or SOSL statement. Bind expressions can be used as given below:
  • Search string in FIND clauses
  • Filter literals in WHERE clauses
  • Numeric value in LIMIT clauses
  • IN or NOT IN operator in WHERE clauses, allowing filtering on a dynamic set of values. Note that this is of particular use with a list of IDs or Strings, though it works with lists of any type.
  • Division names in WITH DIVISION clauses