I'm trying to get the values from sub query result using java code and partner connection, but I'm not able to get the inner records.

I tried using XmlObject. I'm able to use below code in parse the XmlObject and retrieve the relevant data. Still want to know if there is more simple way.

QueryResult queryResult = connection.query("SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account LIMIT 10");

for(SObject acc : queryResult.getRecords()){

    Sytem.out.println("account name: " + acc.getField("Name"));

    XmlObject childsRecords = (XmlObject) acc.getChild("Contacts");

    //Need loop over Contact records
    Iterator<XmlObject> innerChild = childsRecords.getChildren();

        XmlObject contactXml = innerChild.next();

        System.out.println("Contact Id: " + contactXml.getField("Id"));
  • 1
    attachments.getChildren()? Where did you define the attachments variable? That code is very unclear. Mar 16, 2018 at 15:49
  • Sorry. I meant childsRecords.GetChildren(). I update the question.
    – Liron C
    Mar 16, 2018 at 15:52
  • I don't know Java. So I won't place an actual answer. But based on the code you wrote acc.getChild("Contacts") should return a List<sObject> (or java equivalent). Mar 16, 2018 at 15:53
  • Thanks. But that didn't work, as the getChild method return type XmlObject. I tried to cast the return type to List<SObject> but that throw exception "com.sforce.ws.bind.XmlObject cannot be cast to java.util.List"
    – Liron C
    Mar 16, 2018 at 15:56


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.