The following SOSL is in an Account trigger in a managed package:
/* Trigger is on API 27.0 */
String acctsNamesOrd = 'PARKER/VANGUARD MGT';
Map<Integer, String> acctsNames = new Map<Integer, String>{0 => 'PARKER/VANGUARD MGT'};
Map<Integer, String> acctsAddress = new Map<Integer, String>{0 => '15 COMMAND DR. #9'};
Map<Id, Account> acctsChanged = new Map<Id, Account>{'001J000001YAno5' => new Account(Id='001J000001YAno5')};
List<List<Sobject>> contactMatchSosl = [find :acctsNamesOrd in ALL FIELDS
returning Contact(Id, PKG__DBA__c, FirstName, LastName, MailingStreet, AccountId
where (PKG__DBA__c in :acctsNames.values()
or ((LastName in :acctsNames.values() or Name in :acctsNames.values()) and mailingStreet in :acctsAddress.values() and (AccountId in :acctsChanged.keySet() or Account.Name = 'Public')))
and PKG__Confidential__c = false)];
When the package is installed in a client sandbox with 100K+ Contacts we get a QueryException BUT the exact same Search runs fine as part of a custom class: I created an Apex class (API 27.0) in client sandbox and copy/pasted the SOSL in there. Then called it up from Execute Anonymous. I have verified that all variables referenced in the packaged code are non-empty/non-null.
Not just that, the Search also runs fine in Query Editor (in client sandbox):
find {"PARKER/VANGUARD MGT"} in ALL FIELDS returning Contact(Id, PKG__DBA__c, FirstName, LastName, MailingStreet, AccountId where (PKG__DBA__c in ('PARKER/VANGUARD MGT') or ((LastName in ('PARKER/VANGUARD MGT') or Name in ('PARKER/VANGUARD MGT')) and mailingStreet in ('15 COMMAND DR. #9') and (AccountId in ('') or Account.Name = 'Public'))) and PKG__Confidential__c = false)
The Query Plan tool is showing these:
Cardinality | Fields | Leading Op Cost | sObject Cardinality | sObject Type
------------|--------|-----------------|---------------------|-------------
55206 | | TableScan | 118208 | Contact
Not considering filter for optimization because unindexed. Table: Contact Fields: ["IsDeleted"]
Not considering filter for optimization because query table is not optimizable. Table: Account Fields: ["IsDeleted"]
Not considering filter for optimization because unindexed. Table: Contact Fields: ["PKG__Confidential__c"]
Not considering ORed filter for optimization because unindexed. Table: Contact Fields: ["PKG__DBA__c"]
I am not understanding this discrepancy. What's going on?
UPDATE: I was able to repro the error using a custom trigger within the sandbox. As sfdcfox pointed out below running the code in Exec Anon is not the same thing.
PKG__DBA__c
is in the filter I get the error. Remember that this is an OR Search, which means I can't loop through the results to narrow them down. Any ideas?