Search code examples
salesforceapex-codesoql

searching comma separated list using SOSL with wild cards


Is it possible to use a SOSL query to search for Product Names in a comma separated list that still includes wildcards?

For example, if you input a search string like this: productA, productB, productC

Can I run a SOSL query that would return all products for %productA% and %productB% and %productC%?

Is that possible?

Thanks for any help.


Solution

  • Yes, but not comma separated ;)

    "OR" is considered a keyword for searches (similar to SQL and SOQL). Here's a search similar to yours performed on dataset from my Developer Edition.

    Sample search result in Dev. Edition

    List<List<Sobject>> results = [FIND 'Burli* OR Uni*' 
        IN Name FIELDS 
        RETURNING Account (Id, Name) 
        LIMIT 2];
    System.debug(JSON.serializePretty(results[0]));
    
      DEBUG|[ {
      "attributes" : {
        "type" : "Account",
        "url" : "/services/data/v29.0/sobjects/Account/0017000000Lg8WgAAJ"
      },
      "Id" : "0017000000Lg8WgAAJ",
      "Name" : "University of Arizona"
    }, {
      "attributes" : {
        "type" : "Account",
        "url" : "/services/data/v29.0/sobjects/Account/0017000000Lg8WbAAJ"
      },
      "Id" : "0017000000Lg8WbAAJ",
      "Name" : "Burlington Textiles Corp of America"
    } ]
    

    See SOSL reference for more goodies. If the samples on the bottom of the page will look off (all these curly braces) - rememeber that there's slight difference between SOQL called from say SOAP API and the one called from Apex code.