Use WHERE clause in SOSL query

apexdynamic-soslsosl

I am trying to use the existing LWC with some modifications to get records on the basis of RecordType.Id but it's throwing an error instead. System.QueryException: expecting a right parentheses, found 'Y000000Wl0yQAC'.

Apex:

tring searchKeyword = searchTerm + '*';
        
        String returningQuery = '';
        returningQuery = objectName+' ( Id, '+String.join(fields,',')+' WHERE RecordType.Id = '+recordTypeId+' )';
        String query = 'FIND :searchKeyword IN ALL FIELDS RETURNING '+returningQuery+' LIMIT 20';

The resultant String query is FIND :searchKeyword IN ALL FIELDS RETURNING Account ( Id, Rating,AccountNumber,Name WHERE RecordType.Id = 0120Y000000Wl0yQAC ) LIMIT 20

How to fix this??

Best Answer

You need quotes around string values, including ID values:

FIND :searchKeyword 
IN ALL FIELDS 
RETURNING Account ( 
  Id, Rating, AccountNumber, Name 
  WHERE RecordType.Id = '0120Y000000Wl0yQAC') 
LIMIT 20
Related Topic