It seems like SOQL does not have an equivalent to SQL's "SELECT * FROM …"
(If SOQL has this syntax, show me and I will mark that as the answer.)
To get around this limitation, I have been using a modified version of the code found here to generate String based SOQL queries. That code works well, but String based queries require that you convert objects into the correct format.
(here is an example of converting a DateTime object to the correct String representation).
Apex's lack of .toString() methods makes the conversion process unduly difficult, and String.valueOf(obj) rarely provides valid SOQL values.
I would like to create a query using the [SELECT Id, … FROM … ] syntax.
Is there a way to dynamically generate a query using that syntax?
Maybe something like:
String columns = 'Id, Name, AnotherColumn';
List<...> records = [SELECT columns FROM ...];
Best Answer
Absolutely there is. Use the following syntax:
You can also reference instance variables, but not any variable of an instance
And since you mentioned there being no equivalant to
SELECT * FROM ...
in SOQL, here's some code I've written to get around that. You'll need to dig into the code to see how it all works. It's very handy when starting a project.Test Code: