Tag Archives: All Fields

SOQL Query to Select All Fields of sObject

Sample Code:

public class DynamicSOQL {
  public static sObject getAllFieldsData(String objTypeName) {
        sObject sObj;
        Schema.DescribeSObjectResult[] descSObj = Schema.describeSObjects(new String[]{objTypeName});
        Map<String, Schema.SObjectField> fieldMap = descSObj[0].fields.getMap();
        List<String> fieldNames = new List<String>(fieldMap.keySet());
        system.debug('Fields-' + String.join(fieldNames, ','));
        String queryString = 'SELECT '+ String.join(fieldNames, ',') +' FROM '+ objTypeName +' LIMIT 1';
        sObj = Database.query(queryString);
        return sObj;
    }
}