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

时间:2012-01-08 19:15:01

标签: salesforce soql

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

select * from Account [ SQL syntax ]

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

6 个答案:

答案 0 :(得分:24)

像这样创建一个地图:

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 *。

答案 1 :(得分:18)

你必须指定字段,如果你想构建一些动态的,那么describeSObject调用会返回关于一个对象的所有字段的元数据,所以你可以从中构建查询。

答案 2 :(得分:6)

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

答案 3 :(得分:3)

如果有人在寻找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;
}

答案 4 :(得分:1)

对我来说,这是今天第一次使用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;
}

答案 5 :(得分:0)

我使用以下方法获取完整的记录-

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

希望会帮助您!