Accessing Query Results from a Relationship Query from the Partner WSDL with Axis for Java

When accessing records from a relationship query you must use the getObjectValue() method in the Axis library to cast the sub-query or sub-sObject to the appropriate type (QueryResult or sObject, respectively) and then get the names and values from those objects. Below is an example of both a Child-to-Parent and Parent-to-Child query, the SOAP message returned, the Java code used to access the records, and the screen output. Note, for the custom objects used in this example, the Child__c object has a lookup relationship to Parent__c, and the query returns only one Parent__c record with the name “Dad” with only one Child__c record with the name “Little One”. Of course, if you want to display multiple records and sub-records, a series of nested for loops can be used.

Child-to-Parent Relationship Query

Example Query String:

SELECT Parent__r.Name FROM Child__c LIMIT 1

Java Code:

QueryResult qrC2P = binding.query("SELECT Parent__r.Name FROM Child__c LIMIT 1");
SObject So = qrC2P.getRecords()[0];
SObject subSo = (SObject)(So.get_any()[0].getObjectValue());
String subSoName = subSo.get_any()[0].getName(); 
String subSoValue = subSo.get_any()[0].getValue();
System.out.println(subSoName + ": " + subSoValue);

SOAP Message Response:

<?xml version="1.0" encoding="UTF-8"?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns="urn:partner.soap.sforce.com" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:sf="urn:sobject.partner.soap.sforce.com">
     <soapenv:Body>
        <queryResponse>
           <result xsi:type="QueryResult">
              <done>true</done>
              <queryLocator xsi:nil="true"/>
              <records xsi:type="sf:sObject">
                 <sf:type>Child__c</sf:type>
                 <sf:Id xsi:nil="true"/>
                 <sf:Parent__r xsi:type="sf:sObject">
                    <sf:type>Parent__c</sf:type>
                    <sf:Id xsi:nil="true"/>
                    <sf:Name>Dad</sf:Name>
                 </sf:Parent__r>
              </records>
              <size>1</size>
           </result>
        </queryResponse>
     </soapenv:Body>
  </soapenv:Envelope>


Output:

Name: Dad


Parent-to-Child Relationship Query

Example Query String:

SELECT (SELECT Name FROM Parent__c.Children__r) FROM Parent__c LIMIT 1

Java Code:

QueryResult qrP2C = binding.query("SELECT (SELECT Name FROM Parent__c.Children__r)
   FROM Parent__c LIMIT 1");
SObject So = qrP2C.getRecords()[0];
QueryResult subQR = (QueryResult)(So.get_any()[0].getObjectValue());
SObject subSo = subQR.getRecords()[0];
String subSoName = subSo.get_any()[0].getName(); 
String subSoValue = subSo.get_any()[0].getValue();
System.out.println(subSoName + ": " + subSoValue);

SOAP Message Response:

<?xml version="1.0" encoding="UTF-8"?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"   
    xmlns="urn:partner.soap.sforce.com" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sf="urn:sobject.partner.soap.sforce.com">
     <soapenv:Body>
        <queryResponse>
           <result xsi:type="QueryResult">
              <done>true</done>
              <queryLocator xsi:nil="true"/>
              <records xsi:type="sf:sObject">
                 <sf:type>Parent__c</sf:type>
                 <sf:Id xsi:nil="true"/>
                 <sf:Children__r xsi:type="QueryResult">
                    <done>true</done>
                    <queryLocator xsi:nil="true"/>
                    <records xsi:type="sf:sObject">
                       <sf:type>Child__c</sf:type>
                       <sf:Id xsi:nil="true"/>
                       <sf:Name>Little One</sf:Name>
                    </records>
                    <size>1</size>
                 </sf:Children__r>
              </records>
              <size>1</size>
           </result>
        </queryResponse>
     </soapenv:Body>
  </soapenv:Envelope>


Output:

Name: Little One