1

I am trying to retrieve the records from a query that includes a nested query. I am able to retrieve all of the results from all other parts of the query, just not the one from the nested query. The issue may also include that the name of the field is the same as two others, although it holds a different value.

This is my query:

$query = "SELECT Name,Opportunity.Account.Name,CloseDate,(Select Name from Sales_Order_Information__r),Opportunity_ID__c,Product__c,Country__c,Opportunity.End_User_Requested_Delivery_Date__c from Opportunity  Order By Opportunity_ID__c ";

The name from Sales_Order_Information_r is where I am encountering my issue.

This is how I am retrieving results:

$options = new QueryOptions(200);

  $mySforceConnection->setQueryOptions($options);

  $response = $mySforceConnection->query($query);

  !$done = false;


  if ($response->size > 0) {

    while (!$done) {

    foreach ($response->records as $record) {


    echo $record->Name ; //Opportunity Name (works)
    echo $record->Account->Name //Account Name (works)

    echo $record->Sales_Order_Info__r->Name; //Sales_Order_Info Name **(doesn't work)**

The error I am receiving is:

Notice: Undefined property: stdClass::$Sales_Order_Info__r in (Location)

Notice: Trying to get property of non-object in (Location)

Any help would be appreciated.

6
  • Delete comma before from : Opportunity.End_User_Requested_Delivery_Date__c, from Opportunity
    – SF_user
    Commented Aug 25, 2014 at 8:53
  • Sorry, that's not actually there, I deleted some of the content I was pulling before uploading the question because it held private information.
    – CoderDojo
    Commented Aug 25, 2014 at 8:55
  • 1
    You have to iterate through the nested list.i.e. foreach($record->Sales_Order_Infor__r as $item){echo $item->Name;}
    – highfive
    Commented Aug 25, 2014 at 8:58
  • I just tried this, i'm getting an "invalid argument supplied for foreach()" warning
    – CoderDojo
    Commented Aug 25, 2014 at 9:04
  • Check for the object names. I just copied your code and tried to give you the idea. Looks like child object name should be Salesf_Order_Information__r
    – highfive
    Commented Aug 25, 2014 at 9:08

1 Answer 1

2

Since you are having a nested query, the result also is a nested one. You have to iterate through a list of list. Use below code to get the Sales_Order_Information__c field values.

foreach ($response->records as $record) {        
    echo $record->Name ; //Opportunity Name (works)
    echo $record->Account->Name //Account Name (works)

    if(is_array($record->Sales_Order_Information__r){
      foreach($record->Sales_Order_Information__r as $order){
        echo $order->Name;
        // other fields
      }
    }
}
4
  • I'm receiving the same error still..
    – CoderDojo
    Commented Aug 25, 2014 at 10:04
  • workbench.developerforce.com/login.php Use this and check the json response you are getting.
    – highfive
    Commented Aug 25, 2014 at 10:16
  • I have checked workbench, I am receiving a nested response (the field is inside of another field), but I am not receiving anything at all in php.
    – CoderDojo
    Commented Sep 1, 2014 at 14:44
  • Then no issue with the response, you may need check to handle the response correctly in php.
    – highfive
    Commented Sep 2, 2014 at 3:59

You must log in to answer this question.

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