Category Archives: SOQL

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
)