Fields get unwantedly concatenated in Salesforce SOQL query result. Developer nearly loses it

StackOverflow https://stackoverflow.com/questions/8167385

Pregunta

I'm probably missing something quite basic, but I'm getting very confused (and frustrated) with the results I get from my SOQL queries to the Salesforce API.

My query:

Select Id, FirstName, LastName FROM contact

The resulting object (as rendered by print_r):

stdClass Object
(
    [done] => 1
    [queryLocator] => 
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [type] => Contact
                    [Id] => Array
                        (
                            [0] => 0032000000cPd7uAAC
                            [1] => 0032000000cPd7uAAC
                        )
                    [any] => BuzzAldrin
                )
            [1] => stdClass Object
                (
                    [type] => Contact
                    [Id] => Array
                        (
                            [0] => 0032000000cPt1zABC
                            [1] => 0032000000cPt1zABC
                        )
                    [any] => RonnieVanZant
                )
            [2] => stdClass Object
                (
                    [type] => Contact
                    [Id] => Array
                        (
                            [0] => 0032000000cPb60AA
                            [1] => 0032000000cPb60AA
                        )

                    [any] => PollyJeanHarvey
                )
        )
    [size] => 3
)

The first thing I don't get is why "Id" is an array. A strange quirk, but a workaround is not too hard.

The second thing bothers me endlessly more, though: I select for FirstName and LastName and what happens is they get concatenated and returned as a single string value for a field called "any". To avoid the "split it on uppercase letters" advice I already got from my colleagues, I provided an example with both a two-capital first name and a two-capital last name, and anyhow, in reality I need many more (and more formally unpredictable) fields, and they all get added to this "any" property.

Does anyone see what I'm doing wrong? Assuming it's not such a badly written API, that is?

Edit:

Said developer will now go sit in a corner for a few hours, repenting for not having checked for more recent versions of PHP Toolkit. Seems I was using 11.0, whereas there's already a version 20.0. Shame on me, shame on me indeed. Sorry for wasting your time.

¿Fue útil?

Solución

The behavior you are seeing is mostly because of how PHP's SoapClient interprets the results from the API. If you call getLastResponse() on your API connection after you make the query() calls above, you'll see what the actual SOAP messages look like coming back from Salesforce.

As far as the Id array -- its not really an array, but it is listed twice per record (once for the record itself and once as a field), but PHP turns it into an array because it sees it twice. As far as the any, that's happening because PHP is not understanding the namespaced field tags correctly.

As it looks like you found, using the PHP Toolkit can help with these oddities and return sensible objects for you to work with. You might also want to consider looking at Salesforce's REST API, whose results can be directly consumed by json_decode(). For making the HTTP calls to Salesfore, you might be interested in this simple (almost standalone) REST client in a project of mine.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top