Salesforce SOQL describes the table

Is there a way to get a list of all the fields in a table in Salesforce? DESCRIBE myTable does not work, and SELECT * FROM myTable does not work.

+8
salesforce soql
source share
4 answers

Inside Apex, you can get this by executing the following Apex code snippet. If your table / object has the name MyObject__c , then this will give you a set of API names of all the fields of this object that you have access to (this is important, even as a system administrator, if certain fields on your table / object are not visible using security at the field level, they will not be displayed 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 complete this and achieve the SELECT * FROM MYTABLE functionality, you will need to build 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); 
+9
source share

the API call to the SObject description returns all the metadata about the given object / table, including its fields. It is available in the SOAP, REST, and Apex APIs.

+2
source share

Try using Schema.FieldSet

 Schema.DescribeSObjectResult d = Account.sObjectType.getDescribe(); Map<String, Schema.FieldSet> FsMap = d.fieldSets.getMap(); 

full documentation

+1
source share

Have you tried DESC myTable ?

For me, this works great, as well as in basic tips in italics. Take a look:

enter image description here

-4
source share

All Articles