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 

Can get return results for search query

Hello, i tried to create apex code to give out a list of records from the custom object Mediathek__c. I get a searchKey from the component and would like to create a list based on the searchkey Unfortunatly I do not get any results out of it. Could you tell me what to change about my code to get the List of search results?

Kind regards,
Jonathan
 
@AuraEnabled(cacheable=true)
    public static List<List<sObject>> getSearchResult(String searchKey, String TypValue, String ZielgruppeValue, String ThemaValue){
        String findStr = '*'+searchKey+'*';
        
        Boolean isEmptySearch = String.isEmpty(searchKey);
        
       
      String SelectClause = '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)';
    //String SelectClause = 'SELECT Id, Mediathek_ID_Long__c,  Thema__c, ContentDocumentID__c, ContentVersionId__c, Thumbnail_Image__c,Thumbnail_Preview2__c, Name, Bezeichnung__c, Typ__c, Zielgruppe__c, Umfang__c, Bezeichnung_Link__c, Bezeichnung_Search__c FROM Mediathek__c';
      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('Status__c = \'' + SelectedStatus + '\'');
            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.join(WhereClauses, ' AND ');
        
         String WhereClause = String.join(WhereClauses, ' AND ');
           
  


List<List<sObject>> searchResult = Search.query(SelectClause);
        
 system.debug('The searchResult is: ' + searchResult);

    
     return searchResult;
    }

 
SubratSubrat (Salesforce Developers) 
Hello Jonathan ,

Based on your code, it seems you are trying to perform a SOSL (Salesforce Object Search Language) query to search for records in the Mediathek__c custom object based on various search criteria.

There are a few changes you need to make in your code to get the desired search results:

Construct the WhereClause correctly: You are currently using String.join(WhereClauses, ' AND ');, but you are not assigning the result back to the WhereClause variable. Modify that line as follows:
WhereClause = String.join(WhereClauses, ' AND ');
Append the WhereClause to the SelectClause: After constructing the WhereClause, you need to append it to the SelectClause. Update the SelectClause line as follows:
SelectClause += WhereClause;
Use the Database.query method instead of Search.query: Since you are constructing a dynamic SOQL query, you should use the Database.query method instead of Search.query. Modify the line where you execute the query as follows:
List<sObject> searchResult = Database.query(SelectClause);
Adjust the return type of the method: Since you're querying for a single object type (Mediathek_c), you can change the return type of the method to List<Mediathek_c> instead of List<List<sObject>>.
Here's the updated code with the mentioned changes:
@AuraEnabled(cacheable=true)
public static List<Mediathek__c> getSearchResult(String searchKey, String TypValue, String ZielgruppeValue, String ThemaValue) {
    String findStr = '' + searchKey + '';
    Boolean isEmptySearch = String.isEmpty(searchKey);

    String SelectClause = 'FIND :findStr IN ALL FIELDS RETURNING Mediathek_c (Id, Themac, Mediathek_ID_Longc, Name, Bezeichnungc, ContentDocumentIDc, Typc, Zielgruppec, Umfangc, Bezeichnung_Linkc, Bezeichnung_Searchc WHERE Themac=:ThemaValue AND Typc=:TypValue AND Zielgruppec=:ZielgruppeValue AND Inaktiv_c=false)';
    List<String> WhereClauses = new List<String>();
    
    if (searchKey != null) {
        WhereClauses.add(' (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 ');
    SelectClause += WhereClause;

    List<Mediathek__c> searchResult = Database.query(SelectClause);

    system.debug('The searchResult is: ' + searchResult);

    return searchResult;
}
Make sure to test this code and check the debug logs for any errors or unexpected behavior.

Hope this helps !
Thank you.