1// This is the object for which we required data.
2Map<String, Schema.SObjectField> fieldMap = Opportunity.sObjectType.getDescribe().fields.getMap();
3
4// Get all of the fields on the object
5Set<String> fieldNames = fieldMap.keySet();
6
7// Build a Dynamic Query String.
8List<Opportunity> opps = Database.query('select ' + string.join(fieldNames, ',') + ' from Opportunity');
9
1Id rId = 'SomeValidSFDCId';
2
3DescribeSObjectResult describeResult = rId.getSObjectType().getDescribe();
4Map<String, Schema.SObjectField> fieldMap = describeResult.fields.getMap();
5
6// Get all of the fields on the object
7Set<String> fieldNames = fieldMap.keySet();
8
9// Build a Dynamic Query String.
10String soqlQuery = ' SELECT ' + string.join (fieldName, ',') + ' FROM ' + describeResult.getName() + ' Where Id =: rId';
11
12// Need to return a generic list of sObject because we don't necessarily know exactly what the object is.
13List<sObject> record = Database.query(soqlQuery);
14