get all columns from salesforce query code example

Example 1: how to select all fieldsin a soql query

// This is the object for which we required data.
Map<String, Schema.SObjectField> fieldMap = Opportunity.sObjectType.getDescribe().fields.getMap();
  
// Get all of the fields on the object
Set<String> fieldNames = fieldMap.keySet();
 
// Build a Dynamic Query String.
List<Opportunity> opps = Database.query('select ' + string.join(fieldNames, ',') + ' from Opportunity');

Example 2: how to select all fieldsin a soql query

Id rId = 'SomeValidSFDCId';
 
DescribeSObjectResult describeResult = rId.getSObjectType().getDescribe();      
Map<String, Schema.SObjectField> fieldMap = describeResult.fields.getMap();
  
// Get all of the fields on the object
Set<String> fieldNames = fieldMap.keySet();
 
// Build a Dynamic Query String.
String soqlQuery = ' SELECT ' + string.join (fieldName, ',') + ' FROM ' + describeResult.getName() + ' Where Id =: rId';
 
// Need to return a generic list of sObject because we don't necessarily know exactly what the object is.
List<sObject> record = Database.query(soqlQuery);

Tags:

Sql Example