Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example code to convert GoogleDoc into ContentVersion (external file reference)
public class GoogleDocToFilesBatchable implements Database.Batchable<SObject>, Database.Stateful {
private ID externalDataSourceId { get; set; }
public GoogleDocToFilesBatchable( ID externalDataSourceId ) {
this.externalDataSourceId = externalDataSourceId;
public Database.QueryLocator start( Database.BatchableContext context ) {
return Database.getQueryLocator([
id, name, url, ownerId, parentId,
createdById, createdDate, lastModifiedById, lastModifiedDate
public void execute( Database.BatchableContext context, List<GoogleDoc> googleDocs ) {
List<ContentVersion> filesToInsert = new List<ContentVersion>();
for ( GoogleDoc gd : googleDocs ) {
String gdocId = gd.url.substringAfter( '/d/' ).substringBefore( '/' );
filesToInsert.add( new ContentVersion(
contentLocation = 'E',
origin = 'H',
ownerId = gd.ownerId,
title =,
pathOnClient =,
externalDataSourceId = this.externalDataSourceId,
externalDocumentInfo1 = gd.url,
externalDocumentInfo2 = gdocId,
firstPublishLocationId = gd.parentId,
// Enable "Create Audit Fields" in Setup then you can
// preserve the original create/update values and owner if original owner is inactive
createdById = gd.createdById,
createdDate = gd.createdDate,
lastModifiedById = gd.lastModifiedById,
lastModifiedDate = gd.lastModifiedDate
insert filesToInsert;
// optionally, can delete the GoogleDoc bookmarks since now redundant of Files
// delete googleDocs;
public void finish( Database.BatchableContext context ) {
Copy link

We're considering moving our Salesforce google docs to Files (or somewhere :( ...), but it's not clear to me how to execute this batch.
At the moment, I know that we have Notes & Attachments under Google Docs, Notes & Attachments, but I'd like to figure out the scope of the google docs that may be out there, and what would be involved with a conversion if we no longer use Google.
Where do I look to get the externalDataSourceId of the google drive? Using the REST Explorer in Workbench under /services/data/v43.0/connect, I do not have a link to /content-hub, much less /content-hub/repositories.

Copy link

This was very useful for me. I ended up writing a python script, . It is a lot more flexible about parsing urls to get google drive ids.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment