RETURNING is an optional clause that you can add to a SOSL query to specify the information to be returned in the text search result.
If you don’t specify this clause, the default behavior is to return the IDs of all searchable objects in advanced search up to the maximum limit. The maximum limit is specified in the LIMITn clause or 2,000 (API version 28.0 and later), whichever is smaller. The returned IDs include custom objects even if there’s no custom tab. Search results list objects in the order specified in the clause. API version 27.0 and earlier support a maximum of 200 results.
External objects, articles, documents, feed comments, feed items, files, products, and solutions must be specified explicitly in a RETURNING clause to be returned in search results. For example:
Use the RETURNING clause to restrict the results data that is returned from the search() call.
Syntax
In the following syntax statement, square brackets [] represent optional elements that can be omitted. A comma indicates that the indicated segment can appear more than one time.
Object to return. If specified, then the search() call returns the IDs of all found objects matching the specified object. Must be a valid sObject type. You can specify multiple objects, separated by commas. If you specify more than one ObjectTypeName, each object must be distinct; you can’t repeat an ObjectTypeName within a single RETURNING clause. The search() call only returns objects specified in the RETURNING clause.
FieldList
Optional list of one or more fields to return for a given object, separated by commas. If you specify one or more fields, the fields are returned for all found objects. If you want to use relationship fields, the format and supported depth is the same as SOQL.
USING ListView=
Optional clause used to search within a single given object’s list view. Only one list view can be specified. Only the first 2,000 records of the list view are searched, according to the sort order the user has set for the list view. ListView=Recent searches for the most recently accessed items viewed or referenced by the current user.
WHERE
Optional description of how search results for the given object are filtered, based on individual field values. If unspecified, the search retrieves all the rows in the object that are visible to the user. If you want to specify a WHERE clause, you must include a FieldList with at least one specified field. See conditionExpression for more information.
ORDER BY Clause
Optional description of how to order the returned result, including ascending and descending order, and how nulls are ordered. You can supply more than one ORDER BY clause. If you want to specify an ORDER BY clause, you must include a FieldList with at least one specified field.
LIMITn
Optional clause that sets the maximum number of records returned for the given object. If unspecified, all matching records are returned, up to the limit set for the query as a whole. If you want to specify a LIMIT clause, you must include a FieldList with at least one specified field.
OFFSETn
Optional clause used to specify the starting row offset into the result set returned by your query. OFFSET can be used only when querying a single object. OFFSET must be the last clause specified in a query. If you want to specify an OFFSET clause, you must include a FieldList with at least one specified field.
Invalid WHERE syntax (missing FieldList):
1RETURNING Account (WHERE name like 'test')
Valid WHERE syntax:
1RETURNING Account (Name, Industry WHERE Name like 'test')
Invalid ORDER BY syntax (missing FieldList):
1RETURNING Account (ORDER BY id)
Valid ORDER BY syntax:
1RETURNING Account (Name, Industry ORDER BY Name)
Invalid LIMIT syntax (missing FieldList):
1RETURNING Account (LIMIT 10)
Valid LIMIT syntax:
1RETURNING Account (Name, Industry LIMIT 10)
Invalid OFFSET syntax (missing FieldList):
1RETURNING Account (OFFSET 25)
Valid OFFSET syntax:
1RETURNING Account (Name, Industry OFFSET 25)
The RETURNING clause affects whether external objects are searched. For other objects, the RETURNING clause affects what data is returned, not what data is searched. The IN clause affects what data is searched.
FIND {MyAcccount} IN ALL FIELDS RETURNING Account(Id, Name USING ListView=ListViewName)
Apex requires that you surround SOQL and SOSL statements with square brackets to use them in your statements. You can use Apex script variables and expressions when preceded by a colon (:).