Salesforce Dictionary - Free Salesforce GlossarySalesforce Dictionary
Apex

SOQL to Apex Query Converter

Turn a SOQL query into ready-to-paste Apex: an inline [SELECT ...] assignment, or a Database.query() call with the string safely escaped.

SOQL

Apex

Output appears here.

Related dictionary terms

§

About this tool

You have a SOQL query that runs in the Developer Console or Workbench, and now you need it inside an Apex class. This tool wraps the query as Apex for you: either an inline static query assigned to a typed List, or a dynamic Database.query() call with the query string correctly escaped. Name the variable, pick the style, and copy the result straight into your class.

How it works

For the inline form the tool emits a List assignment using the bracketed [SELECT ...] syntax that Apex compiles natively, naming the variable whatever you choose. For the dynamic form it wraps the query in Database.query() and escapes single quotes in the SOQL so the resulting Apex string literal is valid. It does not infer the concrete sObject type for the dynamic form, since Database.query returns a generic List, so you cast as needed. Conversion happens locally on each keystroke.

When to use it
  • Moving a query you prototyped in Workbench into an Apex class without hand-escaping quotes.
  • Switching a static query to a dynamic Database.query() call when you need to build the WHERE clause at runtime.
  • Generating the Apex form of a query for a quick anonymous-Apex script or a unit-test fixture.
§

Frequently asked questions

When should I use inline [SELECT] versus Database.query()?
Use the inline bracketed form whenever the query is static: it is type-safe and counts against query limits the same way. Reach for Database.query() only when you must build the query string dynamically at runtime, and remember dynamic SOQL needs CRUD/FLS care and is more exposed to injection.
Does it protect against SOQL injection?
It escapes single quotes so the generated Apex string is syntactically valid, but that is not a security guarantee. For user-supplied values in dynamic SOQL, use bind variables or String.escapeSingleQuotes on the input in your own code.
Why is the dynamic result a generic List?
Database.query() returns List<sObject> at compile time, so the tool cannot know your concrete type. Cast the result to the specific sObject list (for example (List<Account>)) in your class.