Search code examples
phpsalesforcesoql

Search query, no distinguish special characters, SOQL


I have this piece of code that searches for a Campaign name:

function test_search() {
    $mySforceConnection = new SforceEnterpriseClient();
    $mySoapClient       = $mySforceConnection->createConnection( 'soapclient/wsdl_dev.jsp.xml' );
    $mylogin            = $mySforceConnection->login( USERNAME_TEST, PASSWORD_TEST . SECURITY_TOKEN_TEST );

    $campaign_name = 'A \& B Campaign';

    $search          = 'FIND {' . $campaign_name . '} IN NAME FIELDS RETURNING CAMPAIGN(ID)';
    $searchResult    = $mySforceConnection->search( $search );
    $campaign_id = $searchResult->searchRecords[0]->Id;
    echo '<pre dir="ltr">';
    var_dump( $searchResult );
    var_dump( $campaign_id );
    echo '</pre>';
    error_log( '$searchResult: ' . print_r( $searchResult, true ) );
}

test_search();

The output I get is a campaign ID, when I go to the page that shows that Campaign everything is alright, it shows me what I was looking for.

Now, if I change this:

$campaign_name = 'A \& B Campaign';

Into this:

$campaign_name = 'A \- B Campaign';

and run the function test_search(), I still get the same Campaign ID and I would like the search query to make the distinction!

Any idea why this happens and how to fix?

Thanks, Oded


Solution

  • SOSL is a full-text search engine (I've read somewhere it uses Apache Solr behind the scenes). It'll search for words from your phrase but you didn't specify exact matching (that would be wrapping the term in quotes, FIND {"A \- B Campaign"}). It tries to be smart, skip too short words, account for different suffixes in English grammar etc... It's a fuzzy match engine.

    Or perhaps what you need is plain old SELECT statement? SELECT Id FROM Campaign WHERE Name LIKE '%A & B Campaign%'? If all you need is 1 object and only Name fields... (it'll be query() method from what I remember rather than search())

    If you have access to your SF org then just experiment directly in its Developer Console -> Query Editor for example