function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BlobBlob 

Escaping SOQL

Is there a method provided to escape an SOQL query?
What other characters than ' need to be escaped?

For example:
SELECT Name FROM OPPORTUNITY WHERE Name = 'John Doe's Opportunity'

which will need to be changed to:
SELECT Name FROM OPPORTUNITY WHERE Name = 'John Doe\'s Opportunity'

The 7.0 API documentation maybe should mention more about escaping the query.

fieldExpression Syntax

A fieldExpression uses the following syntax:

fieldName comparisonOperator value

where:


Syntax Description
fieldName The name of a field in the specified object. Use of single or double quotes around the name will result in an error. You must have at least read-level permissions to the field. It can be any field—it does not need to be a field in the fieldList.
comparisonOperator See Comparison Operators for a list of valid operators.
value A value, enclosed in single quotes (double quotes result in an error), used to compare with the value in fieldName. You must supply a value whose data type matches the field type of the specified field. You must supply a native value—other field names or calculations are not permitted. For date values, use the formatting listed in Date Formats.

benjasikbenjasik
there are docs on SOSL about escaping, same thing should apply to SOQL

Reserved Characters

The following characters are reserved for current and future use:

& | ! ( ) { } [ ] ^ " ~ * ? : \ ' + -

In the current version, the following special characters are used:

* ? ( ) "

Reserved characters, if specified in a text search, must be escaped (preceded by the backslash \ character) in order to be properly interpreted. This is true even if the SearchQuery is enclosed in double quotes. An error occurs if you do not precede reserved characters with a backslash. For example, to search for the following text:

(1+1):2

you must escape the reserved characters in the following manner:

\(1\+1\)\:2