function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
J. J. MarkJ. J. Mark 

Retrieving related data from SOQL query -- need alternative to referencing nested objects by number

I'm performing a basic related query:

SELECT Id, OpportunityId, Ship_To_Contact__r.Name, Bill_To_Contact__r.Name FROM Quote

I want to be able to retrieve the results, but I'm not sure how to get the related data because it gets placed in nested objects whose index number can change (see examples below).

The PHP below will work properly for Object #1, but will fail for Object #2 and Object #3 because it is referencing their index numbers. But I don't know another way.

I'm hoping that there is a simple approach that I'm missing here...

Thanks!

Mark

 

----

 

//GOAL: tab-delimited data with consistent positions to be mapped to external data source

 

try {

 

$query = 'SELECT Id, OpportunityId, Ship_To_Contact__r.Name, Bill_To_Contact__r.Name FROM Quote';

 

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

 

$records = $queryResult->records;

 

foreach($records as $record) {

$sObject = new SObject($record);

 

//Opp Id - local to table

try {  

echo $sObject->fields->OpportunityId;

}

catch (Exception $ex){

}

echo "\t";

 

 

//Ship-To Related Data

try {

echo $sObject->fields->{1}->fields->Name;

}

catch (Exception $ex){

}

echo "\t";

 

//Bill-To Related Data

try {

echo $sObject->fields->{2}->fields->Name;

}

catch (Exception $ex){

}

echo "\t";

 

echo "\n";

 

}

 

} catch (Exception $ex) {

print_r($ex);

echo $mySforceConnection->getLastRequest();

echo $e->faultstring;

}

 

-----


FIRST OBJECT RETURNED -- Ship-To is nested object #1 and Bill-To is nested object #2


SObject Object
(
[type] => Quote
[fields] => stdClass Object
(
[OpportunityId] => 006i0000009cSbeAAE
[1] => SObject Object
(
[type] => Contact
[fields] => stdClass Object
(
[Name] => Roger Waters
)

)

[2] => SObject Object
(
[type] => Contact
[fields] => stdClass Object
(
[Name] => Syd Barrett
)

)

)

[Id] => 0Q0i0000000FcbZCAS
)


SECOND OBJECT RETURNED -- See how both Ship-To and Bill-To have become attributes

SObject Object
(
[type] => Quote
[fields] => stdClass Object
(
[OpportunityId] => 006i0000009cSbZAAU
[Ship_To_Contact__r] =>
[Bill_To_Contact__r] =>
)

[Id] => 0Q0i0000000FcbACAS
)


THIRD OBJECT RETURNED -- See how the Ship-To is an attribute and the Bill-To is now nested object #1 instead of #2

SObject Object
(
[type] => Quote
[fields] => stdClass Object
(
[OpportunityId] => 006i000000BNAcqAAH
[Ship_To_Contact__r] =>
[1] => SObject Object
(
[type] => Contact
[fields] => stdClass Object
(
[Name] => Jake Test
)

)

)

[Id] => 0Q0i0000000FykgCAC
)