SOQL Queries: Handling GROUP BY aggregation with the Force.com PHP Toolkit

I needed to run a query and pull a distinct set of values. Easy in Force.com Explorer.
SELECT Field_Name FROM Table_Name GROUP BY Field_Name

Not so easy when querying though, as the result looks like this:
print_r($response->records); // print the response...

Array
(
    [0] => stdClass Object
        (
            [Id] => 
            [any] => <sf:Field_Name>Unique Value</sf:Field_Name>
        )

    [1] => stdClass Object
        (
            [Id] => 
            [any] => <sf:Field_Name>Unique Value 1</sf:Field_Name>
        )

    [2] => stdClass Object
        (
            [Id] => 
            [any] => <sf:Field_Name>Unique Value 2</sf:Field_Name>
        )

Okay… at least the response is unique.
Here’s a foreach to parse the results into a pretty array.
foreach($response->records as $record) {
     $results[] = strip_tags($record->any);
}

Now you have something to work with.
Array
(
    [0] => Unique Value
    [1] => Unique Value 1
    [2] => Unique Value 2
)

1 thought on “SOQL Queries: Handling GROUP BY aggregation with the Force.com PHP Toolkit

  1. Thiago

    This works fine when the result of ANY is unique, but if you have more than one value, using strip_value will not work because there is no way to separate the values.

    Eg:
    any: “Vehicle Name01tj0000001EbUZAA9

    Would be converted to:
    HB20S01tj0000001EbUZAA023

    What would you do in this case?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *