17

SOQLのドキュメントを調べていましたが、「アカウント」などのエンティティのすべてのフィールドデータを取得するためのクエリが見つかりませんでした。

select * from Account [ SQL syntax ]

アカウントのすべてのデータをフェッチするための上記のような構文はSOQLにありますか、または唯一の方法はすべてのフィールドを一覧表示することです(照会するフィールドはたくさんありますが)

4

6 に答える 6

25

次のようなマップを作成します。

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);

スーパーフェルは正しいです。SELECT*を直接実行する方法はありません。ただし、この小さなコードレシピは機能します(テストはしていませんが、問題ないように見えます)。当然のことながら、Force.comは、リソースが明示的に必要な場合にのみプロビジョニングされるマルチテナントアーキテクチャを望んでいます。通常、フィールドのサブセットのみが実際に必要な場合にSELECT*を実行することは簡単ではありません。

于 2012-01-09T01:32:33.963 に答える
18

フィールドを指定する必要があります。動的なものを構築する場合は、describeSObject呼び出しがオブジェクトのすべてのフィールドに関するメタデータを返すため、そこからクエリを構築できます。

于 2012-01-08T19:21:29.157 に答える
7

Force.com Explorerを使用し、スキーマフィルター内で、TableNameの横にあるチェックボックスをクリックすると、すべてのフィールドが選択され、クエリウィンドウに挿入されます。これをすべて入力するためのショートカットとして使用します。コピーしてクエリウィンドウから貼り付けます。お役に立てれば。

于 2013-02-22T04:09:54.080 に答える
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;
}
于 2013-08-26T17:33:24.720 に答える
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;
}
于 2014-09-24T17:55:58.667 に答える
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

希望があなたを助けます!!!

于 2019-01-09T10:20:06.893 に答える