Salesforce SOQL描述表

时间:2012-09-25 15:23:51

标签: salesforce soql

有没有办法获取Salesforce中表格中所有字段的列表? DESCRIBE myTable不起作用,SELECT * FROM myTable不起作用。

4 个答案:

答案 0 :(得分:12)

在Apex中,您可以通过运行以下Apex代码段来实现此目的。如果您的表/对象名为MyObject__c,那么这将为您提供一组您可以访问的该对象上所有字段的API名称(这很重要 - 即使是系统管理员,如果确定的话您的表/对象上的字段通过Field Level Security无法显示,它们将不会显示在此处):

// 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);

要完成此操作并实现SELECT * FROM MYTABLE功能,您需要使用以下字段构建动态SOQL查询:

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);

答案 1 :(得分:2)

describeSObject API调用返回有关给定对象/表的所有元数据,包括其字段。它可以在SOAP,REST&amp; Apex API。

答案 2 :(得分:1)

尝试使用 Schema.FieldSet

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

complete documentation

答案 3 :(得分:-5)

您是否尝试过 DESC myTable

对我而言,它运作良好,它也在斜体的基本提示中。看:

enter image description here