0

We are facing a strange error when running SOSL query in our production environment.

We have a component that runs SOSL queries within a custom object, looking in all text fields

When we run this SOSL in the component we receive 6 results, but, if we run the same SOSL directly in the Developer Console, we receive 29 results... Using the same user

Any idea what could be happening?

Sharing Model is set to public read/write in the object

FIND {test} 
IN ALL FIELDS 
RETURNING Object__c(
    Id, Account__c, RecordTypeId 
    WHERE Account__c = '001XXXXXXXXXXXX'
)

We have a lightning component that invokes an @AuraEnabled method, without cache. The controller class is using with sharing, and the Custom Object has more than 25 million records loaded

({
    retrieveObject: function(component) {
        const action = component.get('c.searchRelatedObjects');
        action.setParams({
            accountId: component.get('v.recordId'),
            keywords: component.get('v.keywords'),
        });
        action.setCallback(this, response => {
            if (response.getState() === 'SUCCESS') {
                console.log(response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
});
public with sharing class ObjectProvider {
    @AuraEnabled
    public static String searchRelatedObjects(Id accId, String keywords) {
        try{
            return JSON.stringify(ObjectSelector.searchByAccIdKeywords(accId, keywords));

        }catch(Exception exc){
            throw new AuraHandledException(exc.getMessage());
        }
    }
}
public class ObjectSelector {
    @AuraEnabled
    public static List<Object__c> searchByAccIdKeywords(Id accId, String keywords) {
        return [
              FIND :keywords IN all fields 
              RETURNING Object__c(Id, Account__c WHERE Account__c = :accountId)
        ][0];
    }
}
6
  • 2
    Would you be able to add some more details about the class of the component?
    – manjit5190
    Commented Mar 18, 2021 at 11:26
  • Hello! Like what kind of details?
    – mnunezdm
    Commented Mar 18, 2021 at 11:44
  • 1
    It is possible that field permissions are affecting this, perhaps ALL FIELDS are limited to those fields that the user running the component has access to. Commented Mar 18, 2021 at 11:48
  • The same user is executing the Anonymous Block & Apex
    – mnunezdm
    Commented Mar 18, 2021 at 11:52
  • 1
    Can you please provide more details here, code of the controller, is it with/without sharing, relevant SOQL query that you compare with SOSL, etc? Commented Mar 18, 2021 at 12:14

1 Answer 1

0

Apparently, we have found the solution, seems that the variable binding in the where clause is incorrectly filtering the results

String keywords = 'test';
Id accountId = '001XXXXXXXXXXXXXXX';

Integer size = [
    FIND :keywords IN all fields
    RETURNING Object__c(Id, Account__c WHERE Account__c = '001XXXXXXXXXXXXXXX')
][0].size();

Integer size2 = [
    FIND :keywords IN all fields
    RETURNING Object__c(Id, Account__c WHERE Account__c = :accountId)
][0].size();

Integer size3 = Search.query('FIND :keywords IN all fields RETURNING Object__c(Id WHERE Account__c = \'' + accountId + '\')')[0].size();

Integer size4 = Search.query('FIND :keywords IN all fields RETURNING Object__c(Id WHERE Account__c = :accountId)')[0].size();

Integer size5 = [
    FIND '{test}' IN all fields
    RETURNING Object__c(Id, Account__c WHERE Account__c = '001XXXXXXXXXXXXXXX')
][0].size();

Integer size6 = [
    FIND '{test}' IN all fields
    RETURNING Object__c(Id, Account__c WHERE Account__c = :accountId)
][0].size();

Integer size7 = Search.query('FIND \'{' + keywords + '}\' IN all fields RETURNING Object__c(Id WHERE Account__c = \'' + accountId + '\')')[0].size();

Integer size8 = Search.query('FIND \'{' + keywords + '}\' IN all fields RETURNING Object__c(Id WHERE Account__c = :accountId)')[0].size();

System.assert(false, size + ' - '+ size2 + ' - ' + size3 + ' - ' + size4 + ' - ' + size5 + ' - ' + size6 + ' - ' + size7 + ' - ' + size8);

The results of the system assert are: 29 - 6 - 29 - 6 - 29 - 6 - 29 - 6 (being 29 the correct result and 6 the incomplete one)

So apparently replacing the :accountId with a '' + accountId + '' does the trick

1
  • Have you ever investigated, in which respect the 6 were different from the others? My best guess: an older LastModifiedDate. I've found a similar discrepancy using the two types of references to a Lookup object (executing in the same order). Only that in my case assigning the String literal resulted in on average fewer and not more results. And that the discrepancy disappeared after some waiting. SOSL query results are based on an index, which is updated in the background. This can take time. There is a SOSL query cache too. Were your tests done with volatile data? Commented Dec 25, 2021 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .