Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Last active July 14, 2023 10:04
Show Gist options
  • Select an option

  • Save douglascayers/d2af04aa597931168a17ce8f408ccbbb to your computer and use it in GitHub Desktop.

Select an option

Save douglascayers/d2af04aa597931168a17ce8f408ccbbb to your computer and use it in GitHub Desktop.
ConnectApi.ContentHub examples of adding and updating files in external data source (e.g. Google Drive or SharePoint Online) before Summer '17
/**
* Developed by Doug Ayers (douglascayers.com)
* Org62 Case 16584902
* GUS W-006274
*
* This test creates a file "new document.txt" with contents "Hello World"
* in an external data source, such as Google Drive or SharePoint Online.
*
* Despite being a unit test it has real side effects in the external system.
*
* Run this test, verify the file was created in the external system, then run ContentHubTest2.
*
* In patch release for Summer '17 the bug this code demonstrated was fixed:
* https://releasenotes.docs.salesforce.com/en-us/summer17/release-notes/rn_connect_in_apex_classes.htm
*/
@isTest
private class ContentHubTest1 {
/**
* ConnectApi methods do not support data-siloed tests.
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/connectAPI_TestingApex.htm
*/
@isTest( seeAllData = true )
static void test_add_repository_item() {
String repositoryId = '0XCj0000000TNJB'; // id of external data source
String folderName = 'My Drive'; // top level folder to use; Google Drive usually "My Drive" and SharePoint Online usually "Documents"
String itemTypeId;
String folderId;
Test.startTest();
// get id of first folder in the repository
// e.g. in SharePoint Online this might be the 'Documents' folder in the site collection
// e.g. in Google Drive this might be the 'My Drive' folder
ConnectApi.RepositoryFolderItemsCollection folderItemsCollection = ConnectApi.ContentHub.getRepositoryFolderItems( repositoryId, 'root' );
for ( ConnectApi.RepositoryFolderItem folderItem : folderItemsCollection.items ) {
System.debug( folderItem );
if ( folderItem.type == ConnectApi.FolderItemType.Folder && folderItem.folder.name == folderName ) {
folderId = folderItem.folder.id;
break;
}
}
// get item type id to create a file
ConnectApi.ContentHubAllowedItemTypeCollection allowedItemTypesCollection = ConnectApi.ContentHub.getAllowedItemTypes( repositoryId, folderId );
for ( ConnectApi.ContentHubItemTypeSummary itemType : allowedItemTypesCollection.allowedItemTypes ) {
System.debug( itemType );
if ( itemType.displayName == 'Document' || itemType.displayName == 'Google Docs' ) {
itemTypeId = itemType.id;
break;
}
}
ConnectApi.ContentHubItemInput itemInput = new ConnectApi.ContentHubItemInput();
itemInput.itemTypeId = itemTypeId;
itemInput.fields = new List<ConnectApi.ContentHubFieldValueInput>();
ConnectApi.ContentHubFieldValueInput nameFieldValueInput = new ConnectApi.ContentHubFieldValueInput();
nameFieldValueInput.name = 'name';
nameFieldValueInput.value = 'new document.txt';
itemInput.fields.add( nameFieldValueInput );
ConnectApi.BinaryInput fileData = new ConnectApi.BinaryInput( Blob.valueOf('Hello World'), 'text/plain', 'new document.txt' );
// This now adds a new file to the external data source
// Despite being a test class, actual production data has been altered in the external system
ConnectApi.RepositoryFolderItem newFolderItem = ConnectApi.ContentHub.addRepositoryItem( repositoryId, folderId, itemInput, fileData );
System.debug( newFolderItem );
Test.stopTest();
}
}
/**
* Developed by Doug Ayers (douglascayers.com)
* Org62 Case 16584902
* GUS W-006274
*
* This test finds an existing file "new document.txt" with contents "Hello World"
* in an external data source, such as Google Drive or SharePoint Online, then
* updates the file to be named "updated document.txt" with contents "Goodnight Moon".
*
* Despite being a unit test it has real side effects in the external system.
*
* Run test ContentHubTest1 first, verify the file was created, then run this test to verify the file gets updated.
*
* In patch release for Summer '17 the bug this code demonstrated was fixed:
* https://releasenotes.docs.salesforce.com/en-us/summer17/release-notes/rn_connect_in_apex_classes.htm
*/
@isTest
private class ContentHubTest2 {
/**
* ConnectApi methods do not support data-siloed tests.
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/connectAPI_TestingApex.htm
*/
@isTest( seeAllData = true )
static void test_update_repository_item() {
String repositoryId = '0XCj0000000TNJB'; // id of external data source
String folderName = 'My Drive'; // top level folder to use; Google Drive usually "My Drive" and SharePoint Online usually "Documents"
String itemTypeId;
String folderId;
String fileId;
Test.startTest();
// get id of first folder in the repository
// e.g. in SharePoint Online this might be the 'Documents' folder in the site collection
// e.g. in Google Drive this might be the 'My Drive' folder
ConnectApi.RepositoryFolderItemsCollection folderItemsCollection = ConnectApi.ContentHub.getRepositoryFolderItems( repositoryId, 'root' );
for ( ConnectApi.RepositoryFolderItem folderItem : folderItemsCollection.items ) {
System.debug( folderItem );
if ( folderItem.type == ConnectApi.FolderItemType.Folder && folderItem.folder.name == folderName ) {
folderId = folderItem.folder.id;
break;
}
}
// find the file named 'new document.txt'
folderItemsCollection = ConnectApi.ContentHub.getRepositoryFolderItems( repositoryId, folderId );
for ( ConnectApi.RepositoryFolderItem fileItem : folderItemsCollection.items ) {
System.debug( fileItem );
if ( fileItem.type == ConnectApi.FolderItemType.File && fileItem.file.name == 'new document.txt' ) {
fileId = fileItem.file.id;
break;
}
}
// get item type id to create a file
ConnectApi.ContentHubAllowedItemTypeCollection allowedItemTypesCollection = ConnectApi.ContentHub.getAllowedItemTypes( repositoryId, folderId );
for ( ConnectApi.ContentHubItemTypeSummary itemType : allowedItemTypesCollection.allowedItemTypes ) {
System.debug( itemType );
if ( itemType.displayName == 'Document' || itemType.displayName == 'Google Docs' ) {
itemTypeId = itemType.id;
break;
}
}
// take an existing file from the external data source and now update it
// this now has changed production data in the external data source
// if the developers/customers aren't careful this can corrupt data and have negative side effects
ConnectApi.ContentHubItemInput itemInput = new ConnectApi.ContentHubItemInput();
itemInput.itemTypeId = itemTypeId;
itemInput.fields = new List<ConnectApi.ContentHubFieldValueInput>();
ConnectApi.ContentHubFieldValueInput nameFieldValueInput = new ConnectApi.ContentHubFieldValueInput();
nameFieldValueInput.name = 'name';
nameFieldValueInput.value = 'updated document.txt';
itemInput.fields.add( nameFieldValueInput );
ConnectApi.BinaryInput fileData = new ConnectApi.BinaryInput( Blob.valueOf('Goodnight Moon'), 'text/plain', 'updated document.txt' );
ConnectApi.RepositoryFileDetail updatedFileItem = ConnectApi.ContentHub.updateRepositoryFile( repositoryId, fileId, itemInput, fileData );
System.debug( updatedFileItem );
Test.stopTest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment