Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
    
Delete comma before from : Opportunity.End_User_Requested_Delivery_Date__c, from Opportunity –  SF_user Aug 25 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 Aug 25 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 Aug 25 at 8:58
    
I just tried this, i'm getting an "invalid argument supplied for foreach()" warning –  CoderDojo Aug 25 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 Aug 25 at 9:08

1 Answer 1

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
      }
    }
}
share|improve this answer
    
I'm receiving the same error still.. –  CoderDojo Aug 25 at 10:04
    
workbench.developerforce.com/login.php Use this and check the json response you are getting. –  highfive Aug 25 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 Sep 1 at 14:44
    
Then no issue with the response, you may need check to handle the response correctly in php. –  highfive Sep 2 at 3:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.