我經歷的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