Is there a way to fetch a list of all fields in a table in Salesforce? DESCRIBE myTable
doesn't work, and SELECT * FROM myTable
doesn't work.

- 3,039
- 6
- 26
- 35
4 Answers
From within Apex, you can get this by running the following Apex code snippet. If your table/object is named MyObject__c
, then this will give you a Set of the API names of all fields on that object that you have access to (this is important --- even as a System Administrator, if certain fields on your table/object are not visible through Field Level Security to you, they will not show up here):
// Get a map of all fields available to you on the MyObject__c table/object
// keyed by the API name of each field
Map<String,Schema.SObjectField> myObjectFields
= MyObject__c.SObjectType.getDescribe().fields.getMap();
// Get a Set of the field names
Set<String> myObjectFieldAPINames = myObjectFields.keyset();
// Print out the names to the debug log
String allFields = 'ALL ACCESSIBLE FIELDS on MyObject__c:\n\n';
for (String s : myObjectFieldAPINames) {
allFields += s + '\n';
}
System.debug(allFields);
To finish this off, and achieve SELECT * FROM MYTABLE
functionality, you would need to construct a dynamic SOQL query using these fields:
List<String> fieldsList = new List<String>(myObjectFieldAPINames);
String query = 'SELECT ';
// Add in all but the last field, comma-separated
for (Integer i = 0; i < fieldsList.size()-1; i++) {
query += fieldsList + ',';
}
// Add in the final field
query += fieldsList[fieldsList.size()-1];
// Complete the query
query += ' FROM MyCustomObject__c';
// Perform the query (perform the SELECT *)
List<SObject> results = Database.query(query);

- 842
- 5
- 11
the describeSObject API call returns all the metadata about a given object/table including its fields. Its available in the SOAP, REST & Apex APIs.

- 18,780
- 4
- 59
- 81
Try using Schema.FieldSet
Schema.DescribeSObjectResult d = Account.sObjectType.getDescribe();
Map<String, Schema.FieldSet> FsMap = d.fieldSets.getMap();

- 5,256
- 10
- 42
- 59
-
2How would I query this with SOQL though? – Maverick Sep 25 '12 at 18:31
-
1I think it is not possible using only SOQL... you can do that using Javascript (or any API implementation language), or Apex as I answered . – Martin Borthiry Sep 25 '12 at 18:50
-
You can see your same question and the same answer here: http://stackoverflow.com/questions/8780413/salesforce-soql-query-to-fetch-all-the-fields-on-the-entity – Martin Borthiry Sep 25 '12 at 18:51
Have you tried DESC myTable
?
For me it works fine, it's also in the underlying tips in italic. Look:

- 1,104
- 13
- 22