我正在浏览 SOQL 文档,但找不到查询来获取实体的所有字段数据,例如 Account ,例如
select * from Account [ SQL syntax ]
SOQL中是否有类似上面的语法来获取帐户的所有数据,或者唯一的方法是列出所有字段(尽管要查询的字段很多)
我正在浏览 SOQL 文档,但找不到查询来获取实体的所有字段数据,例如 Account ,例如
select * from Account [ SQL syntax ]
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 * 并不容易。
You have to specify the fields, if you want to build something dynamic the describeSObject call returns the metadata about all the fields for an object, so you can build the query from that.
我使用 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
希望能帮到你!!!