Friday 22 March 2013




Although it is highly discourage to write a query like this, rather than listing the fields you need to retrieve, it is possible in Apex and this is how:

String table = 'Account';
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ;
Schema.SObjectType s = m.get(table) ;
Schema.DescribeSObjectResult r = s.getDescribe() ;
Map<String, Schema.SObjectField> fields = r.fields.getMap() ;
string soql = '';
for (String fieldName : fields.keyset()) {
    if (soql != '') {
        soql += ', ';
    }
    soql += fieldName;
}
soql = 'SELECT ' + soql + ' FROM ' + table;
System.debug(soql);
List<Account> acs = Database.query(soql);
System.debug(acs.get(0));

0 comments:

Post a Comment

    Links