3

I have to retrieve parent and related child records in a query . The way we do it in SOQL is

SELECT Name, Description__c, (SELECT Description__c, Name FROM Child__r) 
FROM Parent

Similarly I have to perform another query with child and grandchild.

Can we have a similar query in SOSL?

1
  • Please try to include only relevant tags to your question.
    – Adrian Larson
    Jun 11, 2020 at 17:24

1 Answer 1

4

Looks like the answer to this is a simple "No".

SOSL searches multiple SObjects, the default is to search all searchable SObjects and return Ids, but if this were possible you would need to specify an SObject in the RETURNING clause.

The RETURNING Fieldspec documentation tells us that the following is the 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.

RETURNING ObjectTypeName[(FieldList [WHERE conditionExpression] [USING Listview=listview name] [ORDER BY Clause] [LIMIT n] [OFFSET n])] [, ObjectTypeName [(FieldList [WHERE conditionExpression] [ORDER BY Clause] [LIMIT n] [OFFSET n])]]

If it were possible to perform a parent-child subquery, it'd fit into the FieldList slot in that syntax statement, and look something like this

List<List<SObject>> soslResult = [FIND 'test' RETURNING Account(Id, Name, (SELECT Id FROM Opportunities LIMIT 5))];

Further down in the documentation, it doesn't tell us that a subquery is not allowed, but it also doesn't tell us that it is allowed. So we move on to some simple testing, which shows that this syntax has errors in both anonymous apex

Unexpected token '<'.

and when trying to save from the developer console (several errors, but the first is below)

Unexpected token '('.

It could be possible to get all 3 SObjects returned by SOSL if all 3 records contain the same text, but you'd need to do the work of tying the records together yourself (using Apex). Subqueries in SOQL RETURNING clauses are not allowed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .