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.
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.
You can use the meta data available from the API to determine which fields for the object are available to the current user.
See the PHP Toolkit 20.0 DescribeSObject Sample for the Enterprise or Partner WSDL. You want the Name
from the fields array. Append them together with comma separators to make the complete list of accessible fields.
You can use FIELDS() as the complete field list. For example:
SELECT FIELDS(ALL) FROM Account LIMIT 200
SELECT FIELDS(CUSTOM) FROM Account LIMIT 200
SELECT FIELDS(STANDARD) FROM Account
You can also mix FIELDS() with other field names in the field list. For example:
SELECT Name, Id, FIELDS(CUSTOM) FROM Account LIMIT 200
SELECT someCustomField__c, FIELDS(STANDARD) FROM Account
Note: Max Limit is 200 - see documentation here
This SOQL query does not have a limit of 200:
SELECT Label,
QualifiedApiName
FROM FieldDefinition
WHERE EntityDefinition.QualifiedApiName = 'Account'
© 2022 - 2024 — McMap. All rights reserved.