Category Archives: PHP Toolkit

SOQL Queries: Handling GROUP BY aggregation with the PHP Toolkit

I needed to run a query and pull a distinct set of values. Easy in 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...

    [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.
    [0] => Unique Value
    [1] => Unique Value 1
    [2] => Unique Value 2

Creating a ContentDocumentLink to a Custom Object in Salesforce

It’s not possible.

At least not at the time of writing this.

After countless hours, we have determined that it is not possible. We get a pretty blatant error from Salesforce though.

FIELD_INTEGRITY_EXCEPTION, You cannot create a link for this type of entity.

Of course, that wasn’t enough. We tried to link Chatter files, we checked permissions, we did the whole shebang. If you come to this page in your search, and you get a similar error, do yourself a favor and stop. I would LOVE to be able to modify this article and say it’s possible, so if you don’t stop coding, please leave me a comment!

So I don’t leave you empty handed, here is the code that I used to, at the very least, create a file and add it to a Content Library, specified by the $contentLibraryId variable. I clearly use the PHP Toolkit with Salesforce.
$sObject = array();
$sObject[0] = new stdclass();
$sObject[0]->Title = $fileName;
$sObject[0]->PathOnClient = $_FILES["uploaded_file"]["name"];
$sObject[0]->VersionData = $encoded_file;
$sObject[0]->FirstPublishLocationId = $contentLibraryId;
$response = $mySforceConnection->create($sObject, 'ContentVersion');

And of course, the code that created the error…
$query = "SELECT Id FROM ContentDocument WHERE LatestPublishedVersionId = '" . $contentDocumentId . "'";
$response = $mySforceConnection->query($query);
$contentId = $doc_response->records[0]->Id; // note - this is used below!
$sObject = array();
$sObject[0] = new stdclass();
$sObject[0]->ContentDocumentId = $contentId;
$sObject[0]->ShareType = "C";
$sObject[0]->LinkedEntityId = $CustomObjectId;
$response = $mySforceConnection->create($sObject, 'ContentDocumentLink');

This, by the way, is what I’m struggling to achieve: adding files to this “Related Content” area.

Stack Overflow link reference