I want to do something that could be done in SQL:
select * from table
How can this be achieved using SOQL?
I won't be able to use Apex Code, because I run SOQL queries from a PHP page.
By default SOQL
has no support for selecting all fields in this way:
SELECT * From sObject
Check below for a generic class that accepts the object name, and based on that gives a SOQL
query for selecting all fields as a String.
Apex Class:
public inherited sharing class GenerateDynamicSOQLQuery {
public static string getSOQLQuery(String strObjectName) {
if(!String.isBlank(strObjectName)) {
String strQuery = 'SELECT ';
list<Schema.DescribeSObjectResult> objDiscribe = Schema.describeSObjects(new List<String>{strObjectName});
map<String, Schema.SObjectField> objFileds = objDiscribe[0].fields.getMap();
list<String> lstOfFieldNames = new list<String>(objFileds.keySet());
strQuery = strQuery + String.join(lstOfFieldNames, ', ') + ' FROM ' +strObjectName;
return strQuery;
}
else {
return null;
}
}
}
Demo: To obtain the output of the generated query, open an "Execute Anonymous" window and then execute below code:
String strQuery = GenerateDynamicSOQLQuery.getSOQLQuery('Account');
//strQuery += ' WHERE Industry = \'Banking\' LIMIT 1';
strQuery += ' LIMIT 1';
System.debug('Account Records ====> '+Database.query(strQuery));
The query results will be in the debug logs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With