I'm trying to use SOSL to locate Account, Contact, Lead, and Site__c records by Id using a dynamic string of matching ids obtained from Destination__c records. However, I'm not returning any records when the following is run - see below -, and instead only receive back an empty List>(), even though I know the records exist. I tried adding a wild card to the Ids in the string clause in case it was a matter of not being able to match between a 15 char and an 18 char Id, but that didn't seem to have an effect.
private static Map<sObject, Destination__c> getReferenceRecords(List<Destination__c> destinations){
List<Id> referenceIds = new List<Id>();
String refIdsString = '';
/* Add all the valid reference Ids from the destinations to a string to be used in a dynamic SOSL query*/
for(Destination__c destination : destinations){
refIdsString += destination.reference_id__c + '*\\\' OR \\\'';
}
refIdsString = refIdsString.removeEnd('*\\\' OR \\\'');
/*Query all the reference objects */
String query = 'FIND {\\\'' + refIdsString + '*\\\'} IN ALL FIELDS RETURNING Account(Id, Name, ShippingAddress), Contact(Id, Name, MailingAddress),'
+' Lead(Id, Name, Address), Site__c(Id, Name, Street_Address__c, Street_Address_2__c, City__c, State__c, Postal_Code__c, Country__c)';
System.debug(query);
List<List<sObject>> queryResults = (List<List<sObject>>)search.query(query);
System.debug(queryResults);
System.debug( (List<Account>) queryResults[0]);
System.debug( (List<Contact>) queryResults[1]);
System.debug( (List<Lead>) queryResults[2]);
System.debug( (List<Site__c>) queryResults[3]);
}