Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Data & queries

SOQL → Apex String Literal

Wrap a SOQL query as a paste-ready Apex string literal: handles single-quote and backslash escaping for you.

SOQL

Apex

Output appears here.

Related dictionary terms

§

About this tool

Pasting a SOQL query into an Apex string literal is one of those small but error-prone tasks: every embedded single quote needs to become `\'`, every backslash needs to double, and (for `Database.query`) you have to make sure you don't accidentally break a bind variable. This tool wraps any SOQL query in a paste-ready Apex string with the escaping handled for you, ready to drop into a Database.query() call.

How it works

The tool reads the SOQL as a raw string, walks each character once, and emits the Apex-string equivalent: backslashes double, single quotes get a leading backslash, newlines convert to `\n`, and bind variables (`:varName`) are left as Apex string interpolation hints. The output is wrapped in a single pair of quotes so you can copy-paste it directly into a `Database.query(...)` argument or a constant declaration.

When to use it
  • Promoting a Workbench-tested SOQL query into an Apex class without manually re-escaping every quote.
  • Building dynamic SOQL strings where the WHERE clause comes from user input and you need a safe baseline.
  • Generating Apex test fixtures that reproduce production queries verbatim.
§

Frequently asked questions

Should I use Database.query or static SOQL?
Static SOQL (the `[ SELECT ... ]` form) is preferred whenever the query shape is fixed at compile time - it gets type checking and security review for free. Use `Database.query` only when the query has to be assembled at runtime.
Does this protect against SOQL injection?
No tool can. Always combine bind variables (`:userInput`) and `String.escapeSingleQuotes()` for any user-controlled fragment of a dynamic SOQL string. This converter handles literal escaping, not security review.
Can I round-trip the output?
Yes - paste the escaped string back into the input field and the tool will un-escape it.