Print this page

Using a SOSL query with a SubSOQL query

Knowledge Article Number 000187803
Description In SOSL if you are using SOQL to fetch the records to be compared by 'IN' Or 'NOT IN' operator,  the SOSL throws an exception :- System.QueryException: unexpected token: 'common.api.soap.parse.SoapParseNode' :

Example query throwing exception :-  

 
FIND 'Test' IN NAME FIELDS 
	RETURNING Campaign (
		Id WHERE Id IN (
			SELECT CampaignId 
				FROM CampaignMember 
				WHERE ContactId = '003J000000mmEZp'
		) 
	LIMIT 2000)
Resolution The Exception is being thrown by SOSL as it does not support SOQL as a subquery. 

Also the records which are being compared by 'NOT IN' or 'IN'  should be surrounded by single quotes. 
 
Reference Doc :-

   http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/index_Left.htm#StartTopic=Content/sforce_api_calls_sosl_where.htm
 
Example :- Firstly, the SOQL which is being used as a sub query in SOSL should be used separately. 
 
SELECT CampaignId 
FROM CampaignMember 
WHERE ContactId = '003J000000mmEZp'
Secondly, use the records as a string and each record surrounded by single quotes.
 
For Example :- 
FIND 'Test' IN NAME FIELDS 
RETURNING Campaign (Id 
WHERE Id IN ('701J00000006dJNIAY', '701J00000006dJNIAY','701J00000006tzhIAA','701J00000006c5kIAA','701J00000006c5QIAQ') 
LIMIT 2000)

NOTE :- The IDEA to support SOQL as the inner part of SOSL is as follows:

    https://success.salesforce.com/ideaView?id=08730000000JJhX
 




promote demote