function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan Wolff 7Jonathan Wolff 7 

SOSL results in Apex class dont work

Hello,
I want to create a component to show records of a custom object that include my searchkey. Unfortunatly my code does not show any results. Could you gelp me with this topic?

My Logs are these:
User-added image
 
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 ');
           
  

  String SQL = searchResult2 + WhereClause;
        system.debug('Der Query ist: ' + SQL); 
   List<sObject> searchResult = Database.query(searchResult2);
 	
	system.debug('searchKey ' + searchResult); 
    
     return searchResult;
    }

 
Gian Piere VallejosGian Piere Vallejos

Hey, this answer can help you:

https://salesforce.stackexchange.com/questions/185795/sosl-not-able-to-fetch-records-when-called-from-apex-even-though-database-havin (https://salesforce.stackexchange.com/questions/185795/sosl-not-able-to-fetch-records-when-called-from-apex-even-though-database-havin" target="_blank)