I have this below 2 SOSL queries. One which is simple one and 2nd is dynamic.

I am passing the below data to this method. The 1st SOSL is returning the data, but the dyanamic SOSL is returning NULL.

IS there any issue with dynamic SOSL.

List<Id> i = new List<Id>();
List<String> params = new List<String>();
params.add('Is_Child__c = true');
params.add('Type_Of_Location__c = \'Country\'');
List<LookupSearchResult> r = TestClass.searchCountries('Bangalore',i,'Geography__c',params);


    public static List<LookupSearchResult> searchCountries(String searchTerm, List<String> selectedIds, String objectName, List<String> params) {
        system.debug('searchTerm' + searchTerm);
        // Prepare query paramters
        searchTerm += '*';

        // Execute search query
        List<List<SObject>> searchResults = [FIND :searchTerm IN ALL FIELDS RETURNING
                                             Geography__c (Id, Name WHERE Name NOT IN :selectedIds)
                                             LIMIT :MAX_RESULTS];
        if(params.size() > 0) {
            String query;
            query = 'Id,Name ';
            query += ' WHERE Name NOT IN :selectedIds AND' + ' ('+ params[0] +' OR ' +params[1] + ')';            
            query = 'FIND \'' + String.escapeSingleQuotes(searchTerm) + '\'  IN ALL FIELDS RETURNING ' + objectName + '(' + query + ')';
            system.debug('query' + query);
            List<List<sObject>> tempVal = search.query(query);
            system.debug('tempVal' + tempVal);
  • 2
    Can you provide the QUERY generated by system.debug ? – Pranay Jaiswal Jul 29 '19 at 20:19

I've run the SOSL query successfully with different fields and returned records with the dynamic query. I think what is happening is that the additional filtering terms on the second query that are not present on the first, are in fact restricting the results:

params.add('Is_Child__c = true');
params.add('Type_Of_Location__c = \'Country\'');

Add the additional filters to the first query and you will see no results there either. Hopefully this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.