You need to sign in to do that
Don't have an account?
Jonathan 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
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; }
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: Append the WhereClause to the SelectClause: After constructing the WhereClause, you need to append it to the SelectClause. Update the SelectClause line as follows: 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: 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: Make sure to test this code and check the debug logs for any errors or unexpected behavior.
Hope this helps !
Thank you.