我经历的SOQL文档的实体,但找不到查询来获取一个实体的所有现场数据说,帐户,像销售人员SOQL:查询来获取所有字段上
select * from Account [ SQL syntax ]
在SOQL中是否有上述语法来获取帐户的所有数据,或者唯一的方法是列出所有字段(尽管有很多字段需要查询)
我经历的SOQL文档的实体,但找不到查询来获取一个实体的所有现场数据说,帐户,像销售人员SOQL:查询来获取所有字段上
select * from Account [ SQL syntax ]
在SOQL中是否有上述语法来获取帐户的所有数据,或者唯一的方法是列出所有字段(尽管有很多字段需要查询)
您必须指定字段if您希望构建动态的describeSObject调用返回关于对象的所有字段的元数据,因此您可以从中构建查询。
创建地图是这样的:
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 *当实际需要通常只字段的子集。
我使用Force.com Explorer和模式过滤器中,您可以点击旁边的表名的复选框,它会选择所有字段,并插入到你的查询窗口 - 我用这个作为一个快捷方式typeing这一切 - 从查询窗口复制并粘贴。希望这可以帮助。
为了防止有人寻找一个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;
}
好男人布拉德,是工作! – Shazoo 2017-07-24 11:58:58
对我来说这是今天第一次与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;
}
请解释我们正在查看的内容,而不是仅仅发布一段代码。谢谢。 – Andrew 2014-09-24 18:21:16
感谢您的答复。你介意分享一个例子,从describeSObject构建查询。 – Sukhhhh 2012-01-08 19:26:28