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 Mar 18 at 11:26
  • Hello! Like what kind of details? – mnunezdm Mar 18 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. – Menachem Shanowitz Mar 18 at 11:48
  • The same user is executing the Anonymous Block & Apex – mnunezdm Mar 18 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? – Oleh Berehovskyi Mar 18 at 12:14
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

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.