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
benfaustbenfaust 

Retrieving Additional Fields

I posted this question already, but after over a week no one has replied, so I'll rephrase it here. Surely we're not the only ones who have encountered this issue, and it's stalling any further progress.
 
We've been successfully retrieving a list of Lead and Contact results using PHP. We're querying the fields LastName, FirstName, Title, Street, City, State, PostalCode, Country, Phone, Email, and Id.
 
When we add ANY other field, whether it's a standard field such as Fax or Description, or a custom field, the contents of the [any] part of the object changes from a value to an array containing two items.
 
Before Any Additional Field:
stdClass Object
(
    [done] => 1
    [queryLocator] =>
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [type] => Lead
                    [Id] => Array
                        (
                            [0] => 00Q3000000BK8qbEAD
                            [1] => 00Q3000000BK8qbEAD
                        )
                    [any] => BenFaustna /naVT12345ben@eem.tv89723472
                )
...
 
After Any Additional Field:
stdClass Object
(
    [done] => 1
    [queryLocator] =>
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [type] => Lead
                    [Id] => Array
                        (
                            [0] => 00Q3000000BK8qbEAD
                            [1] => 00Q3000000BK8qbEAD
                        )
                    [any] => Array
                        (
                            [0] => BenFaustna /naVT12345ben@eem.tv89723472
                            [1] =>
                        )
                )
...
cwoodcwood
Ben,

I'm not sure if this answers you question, but you can get a much easier-to-use object from your query results by populating an SObject with them. My standard query code looks something like this:

$result = $mySforceConnection->query($queryStr);

// If only one row is returned, make it an array
if(!is_array($result->records)) {
$records[] = $result->records;
} else {
$records = $result->records;
}
if(!is_null($result->records)) {
foreach($records as $record) {
$record = new SObject($record); // now the $record object has some useable structure
var_dump($record); // which you can see here
}
} else {
echo "No results returned.\n";
}

Hope this helps.

Regards,
Charlie
Tran ManTran Man
I believe you mentioned that you were using the PHP 4.x version of the toolkit.  This is a symptom of how SOAP stack handles the query and queryMore calls and returns inconsistent  format.  In the PHP 5 toolkit, it is handled by always returning an array regardless of size.

The code for PHP5 is:


Code:
  private function _handleRecords(& $QueryResult) {
    if ($QueryResult->size > 0) {
      if ($QueryResult->size == 1) {
        $recs = array (
          $QueryResult->records
        );
      } else {
        $recs = $QueryResult->records;
      }
      $QueryResult->records = $recs;
    }
  }
Tran ManTran Man
This was changed in a later release to always return an array.  See my reply to Benfaust.
benfaustbenfaust
I updated to the latest PHP toolkit, and am encountering the same issue.
benfaustbenfaust
On closer inspection, it appears as though when more than the basic fields are included in the query, the "any" part of the object which is an array contains the following:
 
[0] contains the basic fields
[1] contains everything else
ClaiborneClaiborne

I experienced the same thing. I modified the function convertFields in SForcePartnerClient.php. This function is called by the SObject constructor function to parse the "any" string . The new code is pretty simple. Also, it causes no harm if $any is not an array.

Code:

/**
 * Parse the "any" string from an sObject.  First strip out the sf: and then
 * enclose string with <Object></Object>.  Load the string using
 * simplexml_load_string and return an array that can be traversed.
 */
function convertFields($any) {  
    // New code to concatenate components of $any into a single string
  if (is_array($any)) {
      foreach ($any as $a) {
          $new_string = $new_string.$a;
      }
      $any = $new_string;
  }
    // End of new code
  
  $new_string = ereg_replace('sf:', '', $any);
  $new_string = '<Object xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'.$new_string.'</Object>';
  $xml = simplexml_load_string($new_string);
  return $xml;
}


 

benfaustbenfaust
Thanks so much! This modified convertFields function seems to have done the trick.
benfaustbenfaust

On a closer look, it works but only part way. Here's the results:

    [fields] => SimpleXMLElement Object
        (
            [FirstName] => Ben
            [LastName] => Faust
            [Street] => na /
            [City] => na
            [State] => VT
            [PostalCode] => 12345
            [Email] => ben@eem.tv
            [Phone] => 89723472
            [Company] => na
            [MobilePhone] => SimpleXMLElement Object
                (
                )

            [Fax] => SimpleXMLElement Object
                (
                )

            [Description] => SimpleXMLElement Object
                (
                )

            [NumberOfEmployees] => SimpleXMLElement Object
                (
                )

            [SICCode__c] => SimpleXMLElement Object
                (
                )

            [CurrentGenerators__c] => SimpleXMLElement Object
                (
                )

            [emailid__c] => SimpleXMLElement Object
                (
                )

            [Extra_Information__c] => SimpleXMLElement Object
                (
                )

        )

Unfortunately, I don't yet know enough about XML objects to be able to extract whatever information is supposed to be inside them as usable data. Any ideas?

ClaiborneClaiborne

You need to "cast" the resulting data into specific "types" - (string), (float), (int). I am not sure why, but . . .

Try something like this -

$phone = (string) $contact->fields->phone;

benfaustbenfaust

Thanks again, that did the trick.

On an off-topic note, have you had any problems the last couple of days? The code we've been working on for this is in no way connected to our pages which provide administrative login to our system connected with Salesforce, or which submit information to Salesforce from the front end. However, yesterday and today we've been experiencing new errors in reponse to calls which, two days ago, worked perfectly. Does anyone know if the Salesforce system has been changing?

Message Edited by benfaust on 07-27-2006 07:58 AM

SuperfellSuperfell
There have been no software changes on the saleforce side. Can you post a capture of the error and the request XML that causes it.
jrizzojrizzo

Bump...

This is a critical issues that has not yet been resolved, and I'm at wits end.  The result of this "error" is that we cannot insert or upsert data into SF, and in effect, rendering our value proposition null and void.

What do we have to do on our end to elevate this to extreme?  Pls advise!

Ben: would you please provide a more detailed explanation?

 

Joseph Rizzo
President/CEO
PluraPage/iNeoMarketing
Direct Line: 571.203.7081
Company Line: 703.453.9120
Cell: 703.244.8516
Fax: 703.453.9170
AOL IM: joemktg2
www.PluraPage.com

www.iNeoMarketing.com

Message Edited by jrizzo on 08-03-2006 11:26 AM

benfaustbenfaust
Sorry, that last post was supposed to go under the following thread. Could an admin please move?