この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

query コールおよび queryMore コールのサンプル

次の Java および C# の例では、Partner WSDL の query() コールおよび queryMore() コールの使用方法を示します。各例では、クエリのバッチサイズを設定して、250 項目が返されるようにします。次に、query コールを実行して、すべての取引先責任者の名と姓を取得し、返された取引先責任者レコードを反復処理します。取引先責任者ごとに、取引先責任者の名と姓、または名が null の場合は姓のみを出力に書き込みます。最後に、クエリによって返される項目がさらにある場合 (QueryResult.done プロパティの値が false をとる場合) は、queryMore() をコールして、項目の次のバッチを取得し、返されるレコードがなくなるまで処理を繰り返します。

サンプルメソッドを実行するには、Partner WSDL の使用例で提供されている対応する Java または C# テンプレートクラスを使用できます。

Java の例

1public void querySample() {    
2    try {
3        // Set query batch size
4        partnerConnection.setQueryOptions(250);
5        
6        // SOQL query to use 
7        String soqlQuery = "SELECT FirstName, LastName FROM Contact";
8        // Make the query call and get the query results
9        QueryResult qr = partnerConnection.query(soqlQuery);
10        
11        boolean done = false;
12        int loopCount = 0;
13        // Loop through the batches of returned results
14        while (!done) {
15            System.out.println("Records in results set " + loopCount++
16                    + " - ");
17            SObject[] records = qr.getRecords();
18            // Process the query results
19            for (int i = 0; i < records.length; i++) {
20                SObject contact = records[i];
21                Object firstName = contact.getField("FirstName");
22                Object lastName = contact.getField("LastName");
23                if (firstName == null) {
24                    System.out.println("Contact " + (i + 1) + 
25                            ": " + lastName
26                    );
27                } else {
28                    System.out.println("Contact " + (i + 1) + ": " + 
29                            firstName + " " + lastName);
30                }
31            }
32            if (qr.isDone()) {
33                done = true;
34            } else {
35                qr = partnerConnection.queryMore(qr.getQueryLocator());
36            }
37        }
38    } catch(ConnectionException ce) {
39        ce.printStackTrace();
40    }
41    System.out.println("\nQuery execution completed.");         
42}

C# の例

1public void querySample() 
2{
3    try
4    {
5        QueryResult qr = null;
6        binding.QueryOptionsValue = new sforce.QueryOptions();
7        binding.QueryOptionsValue.batchSize = 250;
8        binding.QueryOptionsValue.batchSizeSpecified = true;
9
10        qr = binding.query("SELECT FirstName, LastName FROM Contact");
11
12        bool done = false;
13        int loopCount = 0;
14        while (!done)
15        {
16            Console.WriteLine("\nRecords in results set " + 
17                Convert.ToString(loopCount++)
18                    + " - ");
19            // Process the query results
20            for (int i = 0; i < qr.records.Length; i++)
21            {
22                sforce.sObject con = qr.records[i];
23                string fName = con.Any[0].InnerText;
24                string lName = con.Any[1].InnerText;
25                if (fName == null)
26                    Console.WriteLine("Contact " + (i + 1) + ": " + lName);
27                else
28                    Console.WriteLine("Contact " + (i + 1) + ": " + fName
29                        + " " + lName);
30            }
31
32            if (qr.done)
33                done = true;
34            else
35                qr = binding.queryMore(qr.queryLocator);
36        }
37    }
38    catch (SoapException e)
39    {
40        Console.WriteLine("An unexpected error has occurred: " + e.Message +
41            " Stack trace: " + e.StackTrace);
42    }
43    Console.WriteLine("\nQuery execution completed.");
44}