Tuesday, August 5, 2014

How to use escape sequences with SOQL

So you are trying to pass some quoted string in a SOQL query and hits a error as some thing like this "line break not allowed". Now to over come this you can use the escape sequence provided by salesforce. What it really does is that when ever a string ends by single quote then salesforce mark it as a non break sequence, but when you introduce another quote then it breaks the single string into two if "+" is not used. Ok fare enough but if we need more then one quote then it will be very diffycult to add all those "+" signs. So to do this we can use the below methods:

\n or \N New line
\r or \R Carriage return
\t or \T Tab
\b or \B Bell
\f or \F Form feed
\" One double-quote character
\' One single-quote character
\\ Backslash
LIKE expression only: \_ Matches a single underscore character ( _ )
LIKE expression only:\% Matches a single percent sign character ( % )

Example: database.query('Select Id from Account where Id IN ('0019000000txvCQ','0019000000txvCQ')'); wont work

database.query('Select Id from Account where Id IN (\'0019000000txvCQ\',\'0019000000txvCQ\')'); will work

No comments:

Post a Comment