query コールおよび queryMore コールのサンプル
次の Java および C# の例では、Partner WSDL の query() コールおよび queryMore() コールの使用方法を示します。各例では、クエリのバッチサイズを設定して、250 項目が返されるようにします。次に、query コールを実行して、すべての取引先責任者の名と姓を取得し、返された取引先責任者レコードを反復処理します。取引先責任者ごとに、取引先責任者の名と姓、または名が null の場合は姓のみを出力に書き込みます。最後に、クエリによって返される項目がさらにある場合 (QueryResult.done プロパティの値が false をとる場合) は、queryMore() をコールして、項目の次のバッチを取得し、返されるレコードがなくなるまで処理を繰り返します。
サンプルメソッドを実行するには、Partner WSDL の使用例で提供されている対応する Java または C# テンプレートクラスを使用できます。
Java の例
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void querySample() {
18 try {
19 // Set query batch size
20 partnerConnection.setQueryOptions(250);
21
22 // SOQL query to use
23 String soqlQuery = "SELECT FirstName, LastName FROM Contact";
24 // Make the query call and get the query results
25 QueryResult qr = partnerConnection.query(soqlQuery);
26
27 boolean done = false;
28 int loopCount = 0;
29 // Loop through the batches of returned results
30 while (!done) {
31 System.out.println("Records in results set " + loopCount++
32 + " - ");
33 SObject[] records = qr.getRecords();
34 // Process the query results
35 for (int i = 0; i < records.length; i++) {
36 SObject contact = records[i];
37 Object firstName = contact.getField("FirstName");
38 Object lastName = contact.getField("LastName");
39 if (firstName == null) {
40 System.out.println("Contact " + (i + 1) +
41 ": " + lastName
42 );
43 } else {
44 System.out.println("Contact " + (i + 1) + ": " +
45 firstName + " " + lastName);
46 }
47 }
48 if (qr.isDone()) {
49 done = true;
50 } else {
51 qr = partnerConnection.queryMore(qr.getQueryLocator());
52 }
53 }
54 } catch(ConnectionException ce) {
55 ce.printStackTrace();
56 }
57 System.out.println("\nQuery execution completed.");
58}C# の例
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public void querySample()
18{
19 try
20 {
21 QueryResult qr = null;
22 binding.QueryOptionsValue = new sforce.QueryOptions();
23 binding.QueryOptionsValue.batchSize = 250;
24 binding.QueryOptionsValue.batchSizeSpecified = true;
25
26 qr = binding.query("SELECT FirstName, LastName FROM Contact");
27
28 bool done = false;
29 int loopCount = 0;
30 while (!done)
31 {
32 Console.WriteLine("\nRecords in results set " +
33 Convert.ToString(loopCount++)
34 + " - ");
35 // Process the query results
36 for (int i = 0; i < qr.records.Length; i++)
37 {
38 sforce.sObject con = qr.records[i];
39 string fName = con.Any[0].InnerText;
40 string lName = con.Any[1].InnerText;
41 if (fName == null)
42 Console.WriteLine("Contact " + (i + 1) + ": " + lName);
43 else
44 Console.WriteLine("Contact " + (i + 1) + ": " + fName
45 + " " + lName);
46 }
47
48 if (qr.done)
49 done = true;
50 else
51 qr = binding.queryMore(qr.queryLocator);
52 }
53 }
54 catch (SoapException e)
55 {
56 Console.WriteLine("An unexpected error has occurred: " + e.Message +
57 " Stack trace: " + e.StackTrace);
58 }
59 Console.WriteLine("\nQuery execution completed.");
60}