SOSL Query returning different results in anonymous and class
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]; } }</object__c>
Apparently, we have found the solution, seems that the variable binding in the where clause is incorrectly filtering the outcomes
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 = sosl 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