【发布时间】:2012-02-05 12:21:54
【问题描述】:
我正在浏览 SOQL 文档,但找不到查询来获取实体的所有字段数据,比如 Account
select * from Account [ SQL syntax ]
SOQL中是否有类似上面的语法来获取账户的所有数据,或者唯一的方法是列出所有字段(尽管要查询的字段很多)
【问题讨论】:
标签: salesforce soql
我正在浏览 SOQL 文档,但找不到查询来获取实体的所有字段数据,比如 Account
select * from Account [ SQL syntax ]
SOQL中是否有类似上面的语法来获取账户的所有数据,或者唯一的方法是列出所有字段(尽管要查询的字段很多)
【问题讨论】:
标签: salesforce soql
创建这样的地图:
Map<String, Schema.SObjectField> fldObjMap = schema.SObjectType.Account.fields.getMap();
List<Schema.SObjectField> fldObjMapValues = fldObjMap.values();
然后你可以遍历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);
superfell 是正确的,没有办法直接做 SELECT *。但是,这个小代码配方会起作用(好吧,我还没有测试过,但我认为它看起来不错)。可以理解的是,Force.com 想要一个多租户架构,其中资源仅在明确需要时提供 - 在通常只需要字段子集时执行 SELECT * 并不容易。
【讨论】:
您必须指定字段,如果您想构建动态的内容,describeSObject 调用会返回有关对象所有字段的元数据,因此您可以从中构建查询。
【讨论】:
我使用 Force.com Explorer,在模式过滤器中,您可以单击 TableName 旁边的复选框,它将选择所有字段并插入到您的查询窗口中 - 我使用它作为输入所有内容的快捷方式 -只需从查询窗口复制和粘贴。希望这会有所帮助。
【讨论】:
如果有人在寻找 C# 方法,我可以使用反射并提出以下建议:
public IEnumerable<String> GetColumnsFor<T>()
{
return typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
.Where(x => !Attribute.IsDefined(x, typeof(System.Xml.Serialization.XmlIgnoreAttribute))) // Exclude the ignored properties
.Where(x => x.DeclaringType != typeof(sObject)) // & Exclude inherited sObject propert(y/ies)
.Where(x => x.PropertyType.Namespace != typeof(Account).Namespace) // & Exclude properties storing references to other objects
.Select(x => x.Name);
}
它似乎适用于我测试过的对象(并且与 API 测试生成的列相匹配)。从那里开始,它是关于创建查询的:
/* assume: this.server = new sForceService(); */
public IEnumerable<T> QueryAll<T>(params String[] columns)
where T : sObject
{
String soql = String.Format("SELECT {0} FROM {1}",
String.Join(", ", GetColumnsFor<T>()),
typeof(T).Name
);
this.service.QueryOptionsValue = new QueryOptions
{
batchsize = 250,
batchSizeSpecified = true
};
ICollection<T> results = new HashSet<T>();
try
{
Boolean done = false;
QueryResult queryResult = this.service.queryAll(soql);
while (!finished)
{
sObject[] records = queryResult.records;
foreach (sObject record in records)
{
T entity = entity as T;
if (entity != null)
{
results.Add(entity);
}
}
done &= queryResult.done;
if (!done)
{
queryResult = this.service.queryMode(queryResult.queryLocator);
}
}
}
catch (Exception ex)
{
throw; // your exception handling
}
return results;
}
【讨论】:
对我来说,这是今天第一次使用 Salesforce,我用 Java 想出了这个:
/**
* @param o any class that extends {@link SObject}, f.ex. Opportunity.class
* @return a list of all the objects of this type
*/
@SuppressWarnings("unchecked")
public <O extends SObject> List<O> getAll(Class<O> o) throws Exception {
// get the objectName; for example "Opportunity"
String objectName= o.getSimpleName();
// this will give us all the possible fields of this type of object
DescribeSObjectResult describeSObject = connection.describeSObject(objectName);
// making the query
String query = "SELECT ";
for (Field field : describeSObject.getFields()) { // add all the fields in the SELECT
query += field.getName() + ',';
}
// trim last comma
query = query.substring(0, query.length() - 1);
query += " FROM " + objectName;
SObject[] records = connection.query(query).getRecords();
List<O> result = new ArrayList<O>();
for (SObject record : records) {
result.add((O) record);
}
return result;
}
【讨论】:
我用以下来获取完整的记录-
query_all("Select Id, Name From User_Profile__c")
要获得完整的记录字段,我们必须提及此处提到的那些字段- https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select.htm
希望能帮到你!!!
【讨论】: