+ Start a Discussion
David Roberts 4David Roberts 4 

Meaning of syntax [FIND...RETURNING][0]

Could someone explain the query syntax in
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_methods.htm
which uses
results = (List<Account>)[FIND :searchText RETURNING account(Name, billingpostalcode, Phone)][0];
For a start, I don't know what the [0] is doing.
Also, what are search criteria. It isn't returning every account I'd expect.

An alternative would be:
        results = [SELECT  Name, Phone, billingpostalcode FROM Account where name like :searchText];
although I'd add a '%' to the search text.

Hope someone can explain.
Thanks.
 
karthikeyan perumalkarthikeyan perumal
Hello 

SOSL Query: "results = (List<Account>)[FIND :searchText RETURNING account(Name, billingpostalcode, Phone)][0];"  

 For example : FIND {test}RETURNING account(Name, billingpostalcode, Phone)

Test--earch criteria, if its match the 
returning ID include Id fields. like below

 FIND {test}RETURNING account(id, Name, billingpostalcode, Phone)​

[0]-----> The SOSL search results are returned in a list of lists. Each list contains an array of the returned records.

For exmplae : In this case, the list has two elements. At index 0, the list contains the array of accounts. At index 1 its contain array of contact .

List<List<sObject>> searchList = [FIND 'Wingo OR SFDC' IN ALL FIELDS RETURNING Account(Name), Contact(FirstName,LastName,Department)];  Account[] searchAccounts = (Account[])searchList[0]; Contact[] searchContacts = (Contact[])searchList[1];



Use the Salesforce Object Search Language (SOSL) to construct text searches in the search() call, in Apex statements, in Visualforce controllers and getter methods, or the Schema Explorer of the Eclipse Toolkit.

Unlike SOQL, which can only query one object at a time, SOSL enables you to search text, email, and phone fields for multiple objects simultaneously.


SOQL Query :  results = [SELECT  Name, Phone, billingpostalcode FROM Account where name like :searchText];

Use the Salesforce Object Query Language (SOQL) to construct simple but powerful query strings in the queryString parameter in the query() call, in Apex statements, in Visualforce controllers and getter methods, or in the Schema Explorer of the Force.com IDE.
Similar to the SELECT command in Structured Query Language (SQL), SOQL allows you to specify the source object (such as Account), a list of fields to retrieve, and conditions for selecting rows in the source object. SOQL uses the SELECT statement combined with filtering statements to return sets of data, which may optionally be ordered.


Hope this  will clear. 

Thanks
karthik
Prateek Singh SengarPrateek Singh Sengar
Hi David,
The first syntax is for SOSL, lets break it down for better understanding:
results = (List<Account>)[FIND :searchText RETURNING account(Name, billingpostalcode, Phone)][0];

FIND specifies the keyword for string that needs to be search
:searchText is a binding variable that will contain the string that needs to be searched for
RETURNING specifies  information that should be returned as part of search result. In this case it will return account objects with the three fields specified as output of your SOSL
[0]: This is just a shorthand. The output of the SOSL is a list (which can have mutiple accounts that matches your criteria) by adding [0] the author wants to display just the first element.

Since no IN clause was added in the above query it will search for all fields.

For more details in SOSL Syntax please visit.
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_syntax.htm#sforce_api_calls_sosl_syntax
David Roberts 4David Roberts 4
May thanks, Karthik and Prateek for your quick and clear replies. Now I get it!