You need to sign in to do that
Don't have an account?
Jonathan Wolff 7
No results for SOSL List with where clauses
Hello,
I try to build a Searchbar component to give out a List of custom object (Mediathek__c) records. I try to use an sosl to use a keyword and show the results. Unfortunatly, I do not get any records for my searchKey, even tough the debug says, tat the searchkey enters right into the apex class. Could you help me to improve my code to get a proper List. In addition I get the error: 'unexpected toke:Find' in my debug.
I try to build a Searchbar component to give out a List of custom object (Mediathek__c) records. I try to use an sosl to use a keyword and show the results. Unfortunatly, I do not get any records for my searchKey, even tough the debug says, tat the searchkey enters right into the apex class. Could you help me to improve my code to get a proper List. In addition I get the error: 'unexpected toke:Find' in my debug.
public with sharing class MediathekSearchController { @AuraEnabled(cacheable=true) public static List<sObject> getSearchResult(String searchKey, String TypValue, String ZielgruppeValue, String ThemaValue){ String findStr = '*'+searchKey+'*'; Boolean isEmptySearch = String.isEmpty(searchKey); system.debug('Das searchKey ist: ' + findStr); String searchResult2 = 'FIND :findStr IN ALL FIELDS RETURNING Mediathek__c (Id, Thema__c, Mediathek_ID_Long__c, Name, Bezeichnung__c, ContentDocumentID__c, Typ__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c WHERE Thema__c=:ThemaValue AND Typ__c=:TypValue AND Zielgruppe__c=:ZielgruppeValue AND Inaktiv__c=false)'; system.debug('Das searchResult2 ist: ' + searchResult2); List <String> WhereClauses = new List <String>(); IF (searchKey !=null) { WhereClauses.Add(' WHERE (Bezeichnung__c LIKE \'%' + searchKey + '%\' OR Typ__c LIKE \'%' + searchKey + '%\')'); } IF (ThemaValue !=null && ThemaValue !='Alle') { WhereClauses.Add('Thema__c = \'' + ThemaValue + '\''); } IF (TypValue !=null && TypValue !='Alle') { WhereClauses.Add('Typ__c = \'' + TypValue + '\''); } IF (ZielgruppeValue !=null && ZielgruppeValue !='Alle') { WhereClauses.Add('Zielgruppe__c = \'' + ZielgruppeValue + '\''); } String WhereClause = String.join(WhereClauses, ' AND '); //List<List<sObject>> searchResult = Search.query(SelectClause); String SQL = searchResult2 + WhereClause; system.debug('Der Query ist: ' + SQL); List<sObject> searchResult = Database.query(searchResult2); system.debug('searchKey ' + searchResult); return searchResult; }
n your code, you have a few issues that are preventing you from getting the desired search results. Here are the changes you need to make:
Issue with SOSL syntax: The error you encountered, "unexpected token: Find," is because you are using SOSL syntax within a SOQL query. SOSL and SOQL have different syntax structures. To perform a SOSL search, you should use the FIND statement separately from the query.
Use SOSL FIND statement: Instead of constructing a SOQL query, you need to use the SOSL FIND statement to perform the search. You can build the SOSL query dynamically based on the provided search criteria. Here's an example of how you can do it:
Hope this helps !
Thank you.