No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Understanding Query Results
Query results are returned as nested objects. The primary or “driving”
object of the main SELECT query contains query results of
subqueries.
For example, you can construct a query using either parent-to-child or child-to-parent syntax:
- Child-to-parent:
1SELECT Id, FirstName, LastName, AccountId, Account.Name 2FROM Contact 3WHERE Account.Name LIKE 'Acme%'This query returns one query result (assuming there were not too many returned records), with a row for every contact that met the WHERE clause criteria.
- Parent-to-child:
1SELECT Id, Name, 2 ( 3 SELECT Id, FirstName, LastName 4 FROM Contacts 5 ) 6FROM Account 7 WHERE Name like 'Acme%'This query returns a set of accounts, and within each account, a query result set of Contact fields containing the contact information from the subquery.
Subquery results are like regular query results in that you might need to use queryMore() to retrieve all the records if there are many children. For example, if you issue a query
on accounts that includes a subquery, your client application must handle results from the
subquery as well:
- Perform the query on Account.
- Iterate over the account QueryResult with queryMore().
- For each account object, retrieve the contacts QueryResult.
- Iterate over the child contacts, using queryMore() on each contact's QueryResult.
The following sample illustrates how to process subquery results:
1swfobject.registerObject("clippy.codeblock-2", "9");private void querySample() {
2 QueryResult qr = null;
3 try {
4 qr = connection.query("SELECT a.Id, a.Name, " +
5 "(SELECT c.Id, c.FirstName, " +
6 "c.LastName FROM a.Contacts c) FROM Account a");
7 boolean done = false;
8 if (qr.getSize() > 0) {
9 while (!done) {
10 for (int i = 0; i < qr.getRecords().length; i++) {
11 Account acct = (Account) qr.getRecords()[i];
12 String name = acct.getName();
13 System.out.println("Account " + (i + 1) + ": " + name);
14 printContacts(acct.getContacts());
15 }
16 if (qr.isDone()) {
17 done = true;
18 } else {
19 qr = connection.queryMore(qr.getQueryLocator());
20 }
21 }
22 } else {
23 System.out.println("No records found.");
24 }
25 System.out.println("\nQuery succesfully executed.");
26 } catch (ConnectionException ce) {
27 System.out.println("\nFailed to execute query successfully, error message " +
28 "was: \n" + ce.getMessage());
29 }
30}
31
32private void printContacts(QueryResult qr) throws ConnectionException {
33 boolean done = false;
34 if (qr.getSize() > 0) {
35 while (!done) {
36 for (int i = 0; i < qr.getRecords().length; i++) {
37 Contact contact = (Contact) qr.getRecords()[i];
38 String fName = contact.getFirstName();
39 String lName = contact.getLastName();
40 System.out.println("Child contact " + (i + 1) + ": " + lName
41 + ", " + fName);
42 }
43 if (qr.isDone()) {
44 done = true;
45 } else {
46 qr = connection.queryMore(qr.getQueryLocator());
47 }
48 }
49 } else {
50 System.out.println("No child contacts found.");
51 }
52}
53