salesforce SOQL:查询以获取实体上的所有字段

Suk*_*hhh 16 salesforce soql

我正在浏览SOQL文档,但找不到查询来获取实体的所有字段数据,比如说Account

select * from Account [ SQL syntax ]
Run Code Online (Sandbox Code Playgroud)

在SOQL中是否有类似上面的语法来获取帐户的所有数据,或者唯一的方法是列出所有字段(尽管有很多字段需要查询)

Ada*_*dam 25

创建这样的地图:

Map<String, Schema.SObjectField> fldObjMap = schema.SObjectType.Account.fields.getMap();
List<Schema.SObjectField> fldObjMapValues = fldObjMap.values();
Run Code Online (Sandbox Code Playgroud)

然后,您可以遍历fldObjMapValues以创建SOQL查询字符串:

String theQuery = 'SELECT ';
for(Schema.SObjectField s : fldObjMapValues)
{
   String theLabel = s.getDescribe().getLabel(); // Perhaps store this in another map
   String theName = s.getDescribe().getName();
   String theType = s.getDescribe().getType(); // Perhaps store this in another map

   // Continue building your dynamic query string
   theQuery += theName + ',';
}

// Trim last comma
theQuery = theQuery.subString(0, theQuery.length() - 1);

// Finalize query string
theQuery += ' FROM Account WHERE ... AND ... LIMIT ...';

// Make your dynamic call
Account[] accounts = Database.query(theQuery);
Run Code Online (Sandbox Code Playgroud)

superfell是正确的,没有办法直接做一个SELECT*.但是,这个小代码配方会起作用(好吧,我没有测试过,但我觉得它看起来不错).可以理解的是,Force.com需要一种多租户架构,其中资源仅按明确需要进行配置 - 当通常只需要实际需要一部分字段时,不能轻易地执行SELECT*.


sup*_*ell 18

您必须指定字段,如果要构建动态的describeSObject调用返回有关对象的所有字段的元数据,那么您可以从中构建查询.

  • 感谢您的答复 .你是否介意共享一个例子,从describeSObject构建查询. (2认同)

小智 6

我使用Force.com资源管理器,在架构过滤器中,您可以单击TableName旁边的复选框,它将选择所有字段并插入到查询窗口中 - 我将其用作输入全部的快捷方式 - 只需复制和从查询窗口粘贴.希望这可以帮助.