0

I would want to know how I can display the result from a soql child to parent request? My request is:

$query = "SELECT FirstName, LastName, (select Retreat_Booking__c.Retreat__c from Retreat_Bookings__r)FROM Contact";
$response = $mySforceConnection->query($query);


$queryResult = new QueryResult($response);
while($queryResult->current()->FirstName!='' || $queryResult->current()->LastName!='')
{
    echo $queryResult->current()->FirstName." ".$queryResult->current()->LastName." ".$queryResult->current()-> Retreat_Bookings__r->Retreat_Booking__c->Retreat__c." <BR/>";
    $queryResult->next();
}

The request works and display FirstName and LastName but not Retreat__c. I know the result in the 2nd select is a json and I tried many things, but nothing works.

Is somebody can help me please? Thanks a lot in advance!!

1 Answer 1

0

Here's an example of what the JSON looks like when you perform a parent-child subquery through the API.

Here, I've queried

SELECT Id, (SELECT Id, Name FROM Contacts) FROM Account

where Contacts is the name of the child relationship. This illustrates the same paradigm you have, where you query from a parent to a child and include child properties.

{
  "totalSize": 1,
  "done": true,
  "records": [
    {
      "attributes": {
        "type": "Account",
        "url": "/services/data/v43.0/sobjects/Account/0013600001XXXXeAAI"
      },
      "Id": "0013600001XXXXeAAI",
      "Contacts": {
        "totalSize": 1,
        "done": true,
        "records": [
          {
            "attributes": {
              "type": "Contact",
              "url": "/services/data/v43.0/sobjects/Contact/0033600001a27SFAAY"
            },
            "Id": "0033600001aXXXXAAY",
            "Name": "Tim Testerson"
          }
        ]
      }
    }
  ]
}

Note that the child objects are stored in a property of the parent object, and the name of that property is the relationship name. The content is a list of further sObject JSON structures just like the top-level response.

Further, relationships traversed from the child object are shown with another sObject JSON a property on the child object, with the name being the relationship name, and the content another sObject JSON structure.

In your case, the name of the child relationship is Retreat_Bookings__r. You should find more sObject records under that property in your response, in whatever fashion your PHP connector library translates them from the JSON.

When you do

echo $queryResult->current()->FirstName." ".$queryResult->current()->LastName." ".$queryResult->current()-> Retreat_Bookings__r->Retreat_Booking__c->Retreat__c." <BR/>";

It appears to me that your PHP is looking for an extra layer of indirection that isn't there, and failing to index into an array. Although I do not know PHP, I would expect something more like

$queryResult->current()->Retreat_Bookings__r[0]->Retreat__c

(where [0] is whatever syntax PHP uses for array indexing).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .