user2223387
user2223387

Reputation: 31

Trouble with SalesForce Query Results - PHP Toolkit

I am trying to query my SalesForce database using the PHP API. I am successfully retrieving results, however they are largely useless. When doing a print_r on the results, I receive these results from the following query:

Query:

$query = "SELECT C.Id, C.FirstName, C.LastName, C.Email FROM Contact C WHERE C.Email = '*******@gmail.com'"; 
$response = $client->query($query);

Example print_r output of $response->records:

stdClass Object (
    [type] => Contact
    [Id] => Array
        (
            [0] => ######
            [1] => ######
        )

    [any] => NameTest*********@gmail.com
)

I expect to see a [fields] element in the output object, however I am only seeing my fields as a string with no delimiters in this [any] element. I am using the partner.wsdl.xml and am having no problems pushing data to SF, only retrieving. Any ideas how to get a [fields] element or at least a delimiter in the [any] element?

Upvotes: 2

Views: 3556

Answers (4)

sant
sant

Reputation: 21

$query = "SELECT C.Id, C.FirstName, C.LastName, C.Email FROM Contact C WHERE C.Email = '*******@gmail.com'";

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

    foreach ($response as $record) {
    $sObject = new SObject($record);

    echo $sObject->fields->FirstName;
    echo $sObject->fields->LastName;
    echo $sObject->fields->Email;
    echo $sObject->Id;  //(Id will show like this)

}

Upvotes: 2

Liam
Liam

Reputation: 73

I battled this same problem. I eventually got it working with the following code:

$query = "SELECT C.Id, C.FirstName, C.LastName, C.Email FROM Contact C WHERE C.Email = '*******@gmail.com'";

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

foreach ($response as $record) {
    $sObject = new SObject($record);
    print_r($sObject);
}

Upvotes: 7

ryanbrainard
ryanbrainard

Reputation: 6047

It appears that you're getting back a stdClass instead of an SObject. This should happen automatically for you, but try explicitly passing the record the the new SObject() constructor. Something like this (you'll need to change this to a loop if records is actually an array):

new SObject($response->records);

Now, you should be able to call ->fields on the result.

As far as [any] not showing delimiters, it is because they are XML tags and your are viewing it in a browser, so the tags are being rendered as HTML and disappearing. Try viewing the HTML source of the page and you should see them.

Upvotes: 1

user2223387
user2223387

Reputation: 31

For what it's worth, I was able to retrieve fields by using the "retrieve" method of the API in conjunction with the "query" method. I used the query method to get the SalesForce ID since you can specify your search criteria in query, then used the retrieve function with the specific ID from the query method. Doesn't seem to be the most efficient, but it gets the job done.

See the "Retrieving Records" section.

http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_Toolkit_for_PHP

Upvotes: 1

Related Questions