Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape special characters in ruby string to match Salesforce SOQL requirements

I am developing a Salesforce integration for a Rails application and I ran into an issue of searching/submitting contacts with emails that include special characters like '+'.

Within the Salesforce documentation, they mention that some characters are reserved and a '\' needs to be inserted before each reserved character. Details: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl_find.htm

Given that a '\' is used to escape characters in Ruby, I have not been able to add the '\' to a reserved character without running into issues with the Salesforce API. For those who have implemented a Salesforce integration for Rails, how did you solve the issue of reserved characters within emails? Thanks!

like image 272
ab-CS Avatar asked Nov 25 '25 07:11

ab-CS


1 Answers

You should escape special characters ? & | ! { } [ ] ( ) ^ ~ * : \ " ' + - in your string like this

"[email protected]".gsub(/(\?|&|\||\!|\{|\}|\[|\]|\(|\)|\^|\~|\*|\:|\\|\"|\'|\+|\-)/){|special| "\\" + special }
like image 162
Michael Malov Avatar answered Nov 26 '25 21:11

Michael Malov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!