Search code examples
phpsalesforcesoql

Escaping & (ampersand) in SOQL


I have this piece of code to search for data on Salesforce:

Code:

$campaign_name = 'A & B Campaign';  
$search = 'FIND {'.$campaign_name.'} IN NAME FIELDS RETURNING CAMPAIGN(ID)';
$searchResult = $mySforceConnection->search($search);
var_dump('$searchResult: ' . print_r($searchResult, true));

Error:

mismatched character '&' expecting '}'

I tried to use preg_replace, htmlentities, I keep getting the same error, what can I do to be able to search for strings containing &?

Thanks.


Solution

  • You need a slash, like that: FIND {A \& B} (that's a valid SOSL search by the way ;))

    Scroll to bottom of https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_examples.htm for more info.

    No idea if there's a built-in php function that would do this for you. Worst case create your own utility function with str_replace, preg_replace etc.