This post explains the how to query all fields with soql in apex.
By Default SOQL not Support for selecting All Fileds
SELECT * From sObject
check below generic class this class accepts the object name, based on object name it gives the SOQL query in the form String.
Apex Class:
Demo:
Open Execute Anonymous window execute below code
Happy Learning!!!
By Default SOQL not Support for selecting All Fileds
SELECT * From sObject
check below generic class this class accepts the object name, based on object name it gives the SOQL query in the form 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:
Open Execute Anonymous window execute below code
String strQuery = GenerateDynamicSOQLQuery.getSOQLQuery('Account');
strQuery += ' WHERE Industry = \'Banking\' LIMIT 1';
System.debug('Account Records ====> '+Database.query(strQuery));
Result:Happy Learning!!!
No comments:
Post a Comment