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
pmmennegpmmenneg 

Custom Object/Field Visibility

 Hi all.  I am starting my first application, and am connecting via PHP using the partner.wsdl.  I have created a custom 'Contact' object, and am now trying to pull this data via PHP.  Here is the query:

 

$query = 'SELECT C.Id, C.Full_Name__c
          FROM Contact__c C'; 

$result = $mySforceConnection->query($query); $sObject = new SObject($result->records[0]); print_r($sObject);

 

I have successfully connected and made a query, but the query is simply returning an empty result of (using PHP's print_r):

 

 

SObject Object ( [type] => Contact__c [fields] => [Id] => a )

 

Am I dealing with some kind of visibility issue with my custom object?  I have a number of 'Contacts', and the fields I am querying are correct, but again, no data.

 

Thanks for any pointers/assistance, it is truly appreciated!

 

Best Answer chosen by Admin (Salesforce Developers) 
pmmennegpmmenneg

Of course, hrer is the code I ended up with that can pull out the data I need:

 

 

$query = 'SELECT C.Id, C.Full_Name__c
          FROM Contact__c C'; 

$result = $mySforceConnection->query($query);
for($i = 0; $i < count($result->records); $i++) {
  print_r($result->records[$i]->fields->Full_Name__c); 
}

 

 

All Answers

sfdcfoxsfdcfox

Do you have a managed package? Make sure you include the namespace in your query. Similarly, I would try using the Apex Explorer or Apex Data Loader and trying the exact same query to see what crops up. Also, try turning on the SOAP trace feature in PHP, and generate the request and response XML, and we can try and troubleshoot it from there. The code looks sound from your sample, but obviously there's something odd going on.

 

Edit: As a side note, this post technically should have been in the PHP forum as opposed to General Development.

pmmennegpmmenneg

Hi sfdcfox, thanks so much for the answer.  I can view the custom 'Contact__c' object in the Force.com explorer, and running the above SQL returns the Full_Name__c values for the contacts as expected.

 

Sorry, I'm not sure what a managed package is, so it is likely I don't have that (just a Developer account so that I can test before hitting a clients production data). In terms of including the namespace, what would the namespace be for a custom object... the label of the object?  Any attributes for the object that I should be looking at to ensure that it is properly exposed via the API, or is the visibility in the Force.com Explorer confirmation of this visibility?

 

I will indeed post any future issues that involve any PHp in that forum, just thought as the issue here is a general one (custom object visibility where the language being used isn't the issue), this might be the place for it.

 

A trace reveals... sent XML:

 

 

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:partner.soap.sforce.com">
  <SOAP-ENV:Header>
    <ns1:SessionHeader>
      <ns1:sessionId>
      00DA0000000ZIS7!ARQAQB2zsfaOp5m8zHCS95ZVPhU668LHfNAgYSO5R8ZJYWyvc18LoMIYTeoTmCrmkbK1uHQwvUmfe3UKO7AGMuFHH3SoA2.Z</ns1:sessionId>
    </ns1:SessionHeader>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:query>
      <ns1:queryString>SELECT C.Id, C.Full_Name__c FROM Contact__c C</ns1:queryString>
    </ns1:query>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

And interestingly, the response, which looks to have the data!

 

 

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.partner.soap.sforce.com"> <soapenv:Body> <queryResponse> <result xsi:type="QueryResult"> <done>true</done> <queryLocator xsi:nil="true" /> <records xsi:type="sf:sObject"> <sf:type>Contact__c</sf:type> <sf:Id>a00A0000003ASCUIA4</sf:Id> <sf:Id>a00A0000003ASCUIA4</sf:Id> <sf:Full_Name__c>Bill Smith</sf:Full_Name__c> </records> <records xsi:type="sf:sObject"> <sf:type>Contact__c</sf:type> <sf:Id>a00A0000003ASCZIA4</sf:Id> <sf:Id>a00A0000003ASCZIA4</sf:Id> <sf:Full_Name__c>Jane Doe</sf:Full_Name__c> </records> <size>2</size> </result> </queryResponse> </soapenv:Body> </soapenv:Envelope>

 

Ok, so what am I missing?  Why wouldn't the sObject contain the results from the query, which appear to be sent back via XML?  Sorry, a very basic issue I am sure, but again, any help appreciated.

 

Thanks again,

 

Paul

 

 

 

pmmennegpmmenneg

Follow-up, so it looks like the data is available in the $result variable, a print_r on that reveals:

 

 

QueryResult Object ( [queryLocator] => [done] => 1 [records] => Array ( [0] => SObject Object ( [type] => Contact__c [fields] => stdClass Object ( [Full_Name__c] => Bill Smith ) [Id] => a00A0000003ASCUIA4 ) [1] => SObject Object ( [type] => Contact__c [fields] => stdClass Object ( [Full_Name__c] => Jane Doe ) [Id] => a00A0000003ASCZIA4 ) ) [size] => 2 )

 

 

So I guess I just need to see a working example of how people are properly accessing data from the query results, if not through the sObject.  Anyone have any pointers?

 

Thanks,

 

Paul

tobibeertobibeer

Hi Paul,

 

I would not call myself an experienced php developer, but (how) does php "know" what you are trying to construct when doing this...

 

$sObject = new SObject($result->records[0]);

?

 

How would it know what an SObject is? Assuming the query() method is able to create objects on the fly and bind attributes, does it so by entirely ignoring the sf: name-space? Also, dunno if that's the case, but the trouble could also stem from case-sensitivity issues as you have tried to create an SObject whereas the xml reads sObject.

 

Cheers, Tobias.

sfdcfoxsfdcfox

A managed package is one that AppExchange products use in order to allow in-place upgrades once installed. A namespace is what a managed package uses to keep its fields, objects, and so on separate from the other metadata in the organization. It basically changes a field from "FieldName__c" to "NameSpace__FieldName__c" (Where NameSpace is the designated name). I believe you don't need to actually cast to a SObject, because natively, it's simply a stdClass. The stdClass is an object that stores data in a sort of hash, but is exposed as dynamic properties. You should actually be able to access the data thusly:

 

 

$result->records[0]->Full_Name__c

Note that PHP is case sensitive, so do not use, for example, "full_name__c" instead.

 

pmmennegpmmenneg

Ah, thanks - was aware of what a namespace is, just not int he SF context.  This won't be for an AppExchange product, just a PHP-driven web-screen based off of the client's SF data, so I won't be selling it on the AppExchange, at least at this time.

 

Think I have this resolved now, thanks for your time and patience.

 

Paul

tobibeertobibeer

 


Think I have this resolved now, thanks for your time and patience.

 

Paul


If it doesn't involve too much work, would you mind sharing your actual solution and then mark your reply as such?

 

Cheers, Tobias.

 

pmmennegpmmenneg

Of course, hrer is the code I ended up with that can pull out the data I need:

 

 

$query = 'SELECT C.Id, C.Full_Name__c
          FROM Contact__c C'; 

$result = $mySforceConnection->query($query);
for($i = 0; $i < count($result->records); $i++) {
  print_r($result->records[$i]->fields->Full_Name__c); 
}

 

 

This was selected as the best answer