0

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]);
}
2
  • Are you getting no records in a unit test or in normal use?
    – Keith C
    Commented Jul 10, 2018 at 20:52
  • In normal use. I also tried running just the query in the developer console's query editor with a single id that was statically typed, and it still came back with zero results. : FIND {\'a0B2a000000QQrh*\'} 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)
    – KB145
    Commented Jul 10, 2018 at 21:00

1 Answer 1

1

You can't SOSL by ID values. Even though ID values are always "indexed", these are database indexes, not search indexes. Unfortunately, you're going to need to use up to four different queries to get the results you're looking for, or create four lookup fields on the Destination__c object so you can get all the values in a single query (e.g. SELECT Account__r.Name, Lead__r.Name, ...).

3
  • I had a bad feeling that was going to be the case after I saw the "IN ALL FIELDS" alternate options didn't include "IN ID FIELDS" among them, which seemed like something it ought to have if Ids could be searched on. Thanks!
    – KB145
    Commented Jul 10, 2018 at 21:03
  • Unfortunately we have multiple records with the same name of the same object type, so I wouldn't be able to go the Destination__c lookup field route. Although this is kind of hacky... I suppose I could also create a text field on the aforementioned objects that contained the record's id and search for a match on that field instead. Do you know if SOSL can search against formula fields, or would I have to populate it with a workflow or trigger instead?
    – KB145
    Commented Jul 10, 2018 at 21:08
  • @KB145 Formulas are not indexed, because they're "live" results. You would definitely need a text field to search for each object.
    – sfdcfox
    Commented Jul 10, 2018 at 21:12

You must log in to answer this question.

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