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
lowteklowtek 

Relationship Query result to PHP

Hi!
I'm relatively new to SOQL Relationship queries. I want to list the name of contact as well as the company name which happened to be on two different tables. My query is this..
SELECT c.Name, (SELECT a.Name FROM Company a) FROM Contacts c limit 2

but the result in APEX Explorer shows only one column and i have to colapse the row in oder to see the Company.Name. Is there a way to display it in a column? If this is not possible, how can i pull out the Company.Name and Contacts.Name result and display it in PHP.

thanks
Tran ManTran Man
Partner or Enterprise WSDL?
hemmhemm
First off, I believe your syntax is incorrect on your SOQL.

The SELECT c.Name, (SELECT a.Name FROM Company a) FROM Contacts c limit 1 syntax is used when querying sub-objects (related lists).

When querying parent/sibling objects, you want to use the following.  SELECT Name, Account.Name FROM Contacts limit 1.  Maybe they both work (not sure), but I successfully use the latter.

I am using the Partner WSDL and below is my output of the query above.  As you can see, Relationship fields are held within the sobjects section of a particular record.


Code:
SObject Object
(
    [type] => Contact
    [fields] => SimpleXMLElement Object
        (
            [Name] => Scott Hemmeter
        )

    [sobjects] => Array
        (
            [0] => SObject Object
                (
                    [type] => Account
                    [fields] => SimpleXMLElement Object
                        (
                            [Name] => Arrowpointe Corp.
                        )

                )

        )

)

 
Here's the code I used to produce those results.

Code:
$queryString = "SELECT Name, Account.Name FROM Contact";
$queryString .= " LIMIT 1";
$queryResult = $sfdc->query($queryString);
 
 if ($queryResult->size > 0) {
  $records = $queryResult->records;
  foreach ($records as $record) {
   $sObject = new SObject($record);
   echo '<pre>';
   print_r($sObject);
   echo '</pre>';
  }
 }

 

lowteklowtek
I'm using a Partner WSDL. Thanks
lowteklowtek

Hi Hemm. Thanks for replying to my post. I tried your query (SELECT Name, Account.Name FROM Contact limit 1) at Apex Explorer and the result still shows a single column instead of two (one for account.name and contact.name). I have to click on the [+] to display the account.name. Im not sure if this should be the expected result.

Thanks for the help.


hemm wrote:
First off, I believe your syntax is incorrect on your SOQL.

The SELECT c.Name, (SELECT a.Name FROM Company a) FROM Contacts c limit 1 syntax is used when querying sub-objects (related lists).

When querying parent/sibling objects, you want to use the following.  SELECT Name, Account.Name FROM Contacts limit 1.  Maybe they both work (not sure), but I successfully use the latter.

I am using the Partner WSDL and below is my output of the query above.  As you can see, Relationship fields are held within the sobjects section of a particular record.


Code:
SObject Object
(
    [type] => Contact
    [fields] => SimpleXMLElement Object
        (
            [Name] => Scott Hemmeter
        )

    [sobjects] => Array
        (
            [0] => SObject Object
                (
                    [type] => Account
                    [fields] => SimpleXMLElement Object
                        (
                            [Name] => Arrowpointe Corp.
                        )

                )

        )

)

 
Here's the code I used to produce those results.

Code:
$queryString = "SELECT Name, Account.Name FROM Contact";
$queryString .= " LIMIT 1";
$queryResult = $sfdc->query($queryString);
 
 if ($queryResult->size > 0) {
  $records = $queryResult->records;
  foreach ($records as $record) {
   $sObject = new SObject($record);
   echo '
';
   print_r($sObject);
   echo '
'; } }
 




hemmhemm
In Apex Explorer, you are seeing the expected result.  That's just a weird UI thing with Apex Explorer.  More importantly would be what you need to do in PHP and that's what I was posting about.
lowteklowtek

Thanks Hemm... got your point finally. I tried it in PHP to display it in table however I'm not able to display out the Account.Name correctly. Can you help me with my PHP code? I believe my error is in $sObject->fields->Name, $sObject->fields->Name.

Thanks. I appreaciate you help very much!

//here is my PHP code to display the result in table

require_once ('./soapclient/SforcePartnerClient.php');
require_once ('./soapclient/SforceHeaderOptions.php');

$mySforceConnection = new SforcePartnerClient();
$mySoapClient = $mySforceConnection->createConnection("./soapclient/partner.wsdl.xml");
$mylogin = $mySforceConnection->login(user@example.com, "******");

  $queryString = 'SELECT Name, Account.Name FROM Contact LIMIT 2';
  $queryResult = $mySforceConnection->query($queryString);

 if ($queryResult->size > 0) {

    print ('<hr><table border="1" cellpadding="0" cellspacing="0">');
    print ('<td>Contact Name</td><td>Account Name</td>');
 
  $records = $queryResult->records;
  foreach ($records as $record) {
   $sObject = new SObject($record);

   printf('<tr>' .
   '<td>%s</td><td>%s</td>' .
   '</tr>'. "\r\n", $sObject->fields->Name, $sObject->fields->Name);
 
  }
 }

Here is the output in web browser:

Contact NameAccount Name
ABC CompanyABC Company
John SmithJohn Smith